NOT condition not working
I'm having trouble getting a series of if statements to work: I think I must be misunderstanding something. I'm trying to enable/disable the renderers of different objects using a variable (it's for making sure that certain rooms and objects are hidden at different phases of the game. The statement 'if (phase == 1)' works fine and I can use 'if (phase ==2)' to disable the renderer. What I really want to do though is to disable them if phase is NOT equal to 1. I've tried 'if (phase != 1)' (see below) but that doesn't seem to work. I'd be dead grateful if anyone could tell me why! Cheers!
void Update ()
{
GameObject door2 = GameObject.Find("room_2_door");//finding room2 objects and animators
GameObject room2 = GameObject.Find ("room_2");
Animator door2anim = door2.GetComponentInChildren<Animator>();
if (phase == 1) //room 2 visible,
{
door2anim.SetTrigger ("close");
Renderer[] room2childrenrenderer = room2.GetComponentsInChildren<Renderer> ();
foreach (Renderer r2cr in room2childrenrenderer)
{
r2cr.enabled = true;
}
}
if (phase !=1 ) // room 2 not visible
{
door2anim.SetTrigger ("go");
Renderer[] room2childrenrenderer = room2.GetComponentsInChildren<Renderer> ();
foreach (Renderer r2cr in room2childrenrenderer)
{
r2cr.enabled = false;
}
}
Use Debug.Log in both ifs to make sure phase is what you think it is and that you enter the right if.
What do you mean by "doesn't work"? It gets done when not supposed to, doesn't get done when supposed to or both?
If you want something done when phase == 1 and something else done when it's not, you can simply do
if (phase == 1) {
//something
} else {
//something else
}
Another improvement I would suggest would be not to trigger animations and do all this 60 times every second in update, but ins$$anonymous$$d make a SetPhase() method that you'd use to change phase and do all of this only once whenever phase changes
Answer by Blue-Cut · Sep 07, 2016 at 08:56 PM
First of all, you could simplify a bit your code to avoid mistakes. The code bellow does the same thing than your previous code. If you want to focus on your problem without changing your code, just look at the Update part.
As NoseKills said, check with the Debug.Log instruction that 'phase' is not equal to 1 :
// Declare variables (data to keep all the time in your script)
GameObject door2;
GameObject room2;
Animator door2anim;
Renderer[] room2childrenrenderer;
void Start()
{
// Get the value for each variables just once in Start
door2 = GameObject.Find("room_2_door");
room2 = GameObject.Find ("room_2");
door2anim = door2.GetComponentInChildren<Animator>();
room2childrenrenderer = room2.GetComponentsInChildren<Renderer> ();
}
void Update ()
{
// This is to print a line in the Unity console so you can check your values
Debug.Log("phase = "+phase);
// Set your trigger with a if/else
if (phase == 1)
{
door2anim.SetTrigger ("close");
}
else
{
door2anim.SetTrigger ("go");
}
foreach (Renderer r2cr in room2childrenrenderer)
{
// Instead of true/false, you can affect directly your condition to avoid repeating
r2cr.enabled = (phase == 1);
}
}