c# - Throwing Exceptions in Unity -


i'm working on simple script utilizes canvas inputfield , button. when player presses button, script checks text of inputfield. problem having if nothing entered, unity exits unassignedreferenceexception.

i tried catching exception must doing horribly wrong:

public class quiz : monobehaviour {      public gameobject quizpanel;     public gameobject input;      public void checkanswer(){          text answer = (input.getcomponent<text>()) text;          try {             if (answer.text == "george washington") {                 debug.log("true");             }         }catch (unassignedreferenceexception)         {             debug.log ("wrong answer");         }     } } 

i have reasons believe input variable somehow invalid. i'll advise make try/catch encapsulate of code:

public void checkanswer(){     try {         text answer = (input.getcomponent<text>()) text;          if (answer.text == "george washington") {             debug.log("true");         }     }catch (unassignedreferenceexception)     {         debug.log ("wrong answer");     } } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -