- Home /
if else statement ignored ??
I've been up for too long already and I need some fresh eyes to look at this simple snippet of code and see if I am doing anything too stupid here.
First off let me explain what I am trying to accomplish. I am making a simple temple run type endless running game and am just in the preliminary stages of figuring some mechanics out. What I am doing right now is moving cubes representing the ground you are running on to just past the screen view. Once the cube reaches this point a random number is called and if the number is 0 it will make the cube transparent and stop the physics on the cube so that the player can fall through it.
The problem I am having is that Unity will apparently ignore my if else logic preventing the creation of two transparent blocks at once.
This is a problem since my "player" can't jump two spaces at once. Here is the if else statement.
if (transform.position.z < 0)
{
rnd = Random.Range(0, 5);
if(rnd == 0)
{
if(madeHole == false)
{
renderer.enabled = false;
collider.isTrigger = true;
madeHole = true;
}
else
{
renderer.enabled = true;
collider.isTrigger = false;
madeHole = false;
}
}
else
{
renderer.enabled = true;
collider.isTrigger = false;
madeHole = false;
}
transform.position.z += 150;
}
Any help or insight could be useful. Thank you.
Answer by Fattie · Mar 06, 2013 at 10:55 AM
First of all,
you can be utterly certain that there is NO problem with the if statement in Javascript, Mono or Unity3D.
You've made some mistake.
Add debugging statements:
Debug.Log("I am here, point A");
and for example ..
Debug.Log("Now the value of blah is ... " + x );
In this way you can learn more and solve the problem.
When you are learning to program you need to constantly include Debug.Log statements. Essentially every other line of code should be a Debug line.
You'll have the problem solver within an hour, cheers
Thank you for the insight. After some well needed rest I came back to it and realized that since I had attached the script to each individual block to get it to move they all had their separate madeHole bool so that when the next block came through it would use it's own bool which was of course false. I fixed it by making madeHole a global variable in another script. Thank you again.
Your answer
Follow this Question
Related Questions
I'm wondering with if function working 2 Answers
MouseLook half disabling 1 Answer
Gap in requirements in GUI based game 2 Answers
Why is an action in my "if" block executing in the "else" conditions? 2 Answers
Power-Ups scripting error 1 Answer