- Home /
The question is answered, right answer was accepted
If statement not working
Hello, I'm having trouble getting my if statements to start.
if(Wave1 > 0)
{
Debug.Log("Check");
}
Here I want it when Wave1 is set to the value of 1, it will Debug.Log("Check");
function OnTriggerEnter ()
{
SurvivalScript.Wave1 += 1;
}
This is a different script which will add 1 to the Wave1 value.
I want it so it adds 1 to the value when you enter the trigger, then logs ("Check"). Wave1 is a static int and I'm getting no scripting errors, yet I'm getting nothing in the console.
Please help me, Thanks.
add the Debug.Log("Check")
inside the function OnTriggerEnter ()
first to make sure collision is happening
I believe this is correct. Error should be somewhere else. $$anonymous$$aybe show your variable declarations.
Answer by ProgrammerJosh · Aug 16, 2017 at 07:03 AM
Your if statement is in a function called Survival, which is called from the Start method. In Unity, the Start method is only called once when the game object is constructed. I think that your OnTriggerEnter method is being called AFTER the SurvivalScript's Start method has been called, so the if statement never has a chance to respond to the wave increasing
I think what you're trying to do is to poll the state every frame. Try updating your SurvivalScript to this:
static var Wave1 : int = 0;
function Update()
{
Survival();
}
function Survival ()
{
if(Wave1 > 0)
{
Debug.Log("Check");
}
}
Just be aware that the Update method will be called every frame, so you will get a lot of Debug.Log statements printing.
Good luck friend!
Answer by Rick_Swash · Aug 15, 2017 at 10:22 PM
Check if the OnTriggerEnter function actually gets called
The collision with the trigger is happening, read the replies on the post.
Follow this Question
Related Questions
Integer in if statement, scripting help 1 Answer
What is causing my AI script to interpret wrong data? 1 Answer
GUI Counter Based on Animation or RaycastHit 0 Answers
Javascript array questions. 1 Answer
Remove int[] element 1 Answer