i got a simple task : two boxes, one rail, when i put this rail on both of the boxes another lvl is loaded.
so the script on the first box:
var firstBox : boolean = false;
function OnCollisionEnter(Collision : UnityEngine.Collision){
if(Collision.gameObject.tag == "rail"){
//Debug.Log("box1");
firstBox = true;
}
}
on the second box:
var secondBox : boolean = false;
function OnCollisionEnter(Collision : UnityEngine.Collision){
if(Collision.gameObject.tag == "rail"){
//Debug.Log("DOOO2");
secondBox = true;
}
}
and on the empty object:
var boxone : boxCollision;
var boxtwo : box2Collision;
var levelCode : int = 4;
function Update () {
if((boxone.firstBox == true) (boxtwo.secondBox == true));
Debug.Log("change lvl");
}
but when i start the lvl i’m receiving “change lvl”, but the rail was not colliding with any of the boxes… Why does it happens?
tomvds
September 7, 2010, 3:49pm
2
if((boxone.firstBox == true) (boxtwo.secondBox == true));
Debug.Log("change lvl");
Your if statement ends in a semi-colon, which turns it into a useless ‘if (something) do nothing’ statement.
Removing the semi-colon should fix it. You should probably have a warning in your console saying “Possible mistaken empty statement”. It is often a very good idea to treat warnings as errors and fix them right away, or you might miss important warnings like this one.
if((boxone.firstBox == true) (boxtwo.secondBox == true));
Debug.Log("change lvl");
Your if statement ends in a semi-colon, which turns it into a useless ‘if (something) do nothing’ statement.
Removing the semi-colon should fix it. You should probably have a warning in your console saying “Possible mistaken empty statement”. It is often a very good idea to treat warnings as errors and fix them right away, or you might miss important warnings like this one.
That’s the point i got no errors, just debugging “change lvl”
?
Why don’t you write collisionInfo : Collision, like all other scripts ?
tomvds
September 7, 2010, 6:37pm
5
Hmm, well, I normally use C#, which does give the warning. Perhaps it’s not implemented for JS.
Sycle
September 7, 2010, 9:54pm
6
That’s why he said you may have got a ‘warning’ not ‘error’. Your code would still run because technically it’s valid, it just doesn’t do what you think it should do.
To illustrate what has already been said.
if((boxone.firstBox == true) (boxtwo.secondBox == true));
Debug.Log("change lvl");
Is the same as writing
if((boxone.firstBox == true) (boxtwo.secondBox == true))
{
// Do nothing
}
Debug.Log("change lvl");
So of course change lvl is going to appear all the time.
You want to write
if((boxone.firstBox == true) (boxtwo.secondBox == true))
Debug.Log("change lvl");
(note the removal of the semi colon) or
if((boxone.firstBox == true) (boxtwo.secondBox == true))
{
Debug.Log("change lvl");
}