c# - Unity 5 unresponsive after a piece of code is executed -


i absolute beginner in unity. have been working on ui simple login form. in have 2 toggle selecting gender i.e male or female. have been doing calling method checks if male selected remove check male when other toggle pressed.

my unity freezes when click female toogle. here code.

toggle female; toggle male; void start(){     female = gameobject.find ("toggle_female").getcomponent<toggle> ();      male = gameobject.find ("toggle_male").getcomponent<toggle> (); }   public void ismale(){     if (female.ison)         female.ison = false;      male.ison = true;  }  public void isfemale(){     if (male.ison)         male.ison = false;      female.ison = true; } 

you running infinite loop toggle.

when female toggle changes, onvaluechanged event triggered. assume isfemale() function linked toggle onvaluechanged event, isfemale() called.

when isfemale() called, male.ison = false; causing ismale() function called run female.ison = false; causing isfemale() called again. everlasting.

the onvaluechanged event should never fired when ison set script. problem.

solution:

you looking togglegroup.

1.create male toggle gameobject -> ui -> toggle name male toggle.

2.create female toggle gameobject -> ui -> toggle name female toggle. can change label text later on.

3.create empty gameobject. gameobject -> create empty. rename togglegroup.

select newly created gameobject, go component -> togglegroup. now, have gameobject called togglegroup togglegroup component attached it.

4.select male toggle, drag togglegroup gameobject male toggle's group slot. select female toggle, drag togglegroup gameobject female toggle's group slot

select female toggle, unckeck is on checkbox make sure one(male toggle) selected default. click play , now, 1 toggle can selected @ time.

enter image description here


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -