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
Post a Comment