- Home /
Script error: respawn upon collission
Hello!
I am trying to write a script where the player (in this case just a sphere) respawns (as in, goes back to it's original position) when it falls off-world. I wrote the following script:
#pragma strict
var position : Vector3;
function OnCollisionEnter(collision:Collision)
{ if( collision.gameObject.tag == "Finish" );
{transform.position : Vector3(0, 0, 0)};
}
When trying to execute it, I get the following error: Assets/Die.js(5,1): BCE0034: Expressions in statements must only be executed for their side-effects.
What is going wrong?
Answer by BiG · Apr 28, 2012 at 07:49 PM
Try with:
#pragma strict
var position : Vector3;
function OnCollisionEnter(collision:Collision)
{
if( collision.gameObject.tag == "Finish" )
transform.position = Vector3(0, 0, 0);
}
You have substituted an "=" with a ":", and this won't change the position. Also, you've ended the if statement with a ";". It should work now.
Answer by Woutervd · Apr 30, 2012 at 07:16 PM
Thank you! I ended up using
#pragma strict
var position : Vector3;
function OnCollisionEnter(collision:Collision)
{ if( collision.gameObject.tag == "Finish" )
{
transform.position = Vector3(1,5,-2.8);
rigidbody.velocity = Vector3(0,0,0);
}
}
Which works great as a very basic respawn system. The biggest problem was indeed ending the if-statement with a semicolumn. This caused the script to not actually execute what was defined in the function.
Your answer
Follow this Question
Related Questions
How would I move an Object after collision? 1 Answer
Trouble with a respawn script 1 Answer
Adding sound javascript in unity2d 1 Answer
Object Collision PLEASE HELP!!! 1 Answer