Unity 5 unresponsive after a piece of code is executed

前端 未结 2 684
梦毁少年i
梦毁少年i 2021-01-25 21:53

I am a absolute beginner in unity. I have been working on an UI which is a simple login form. In which I have two Toggle for selecting gender i.e male or female. Wh

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 22:16

    I guess the two functions isMale and isFemale are Toggles' event handlers. According to Unity docs:

    The Toggle has a single event called On Value Changed that responds when the user changes the current value

    So the problem is that these event handlers respond to value change and not toggle being checked, so your code causes an infinite loop. It should work by reading them first to make sure it's a check and not uncheck:

     public void isMale(){
        if (!male.isOn) return;
    
        if (female.isOn)
            female.isOn = false;
    
        male.isOn = true;
    
    }
    
    public void isFemale(){
        if (!female.isOn) return;
    
        if (male.isOn)
            male.isOn = false;
    
        female.isOn = true;
    }
    

提交回复
热议问题