x


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?

more ▼

asked Apr 28, 2012 at 07:42 PM

Woutervd gravatar image

Woutervd
25 6 4 5

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 28, 2012 at 07:49 PM

BiG gravatar image

BiG
5.6k 12 20 64

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 30, 2012 at 07:16 PM

Woutervd gravatar image

Woutervd
25 6 4 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5883
x4023
x266
x204

asked: Apr 28, 2012 at 07:42 PM

Seen: 870 times

Last Updated: Apr 30, 2012 at 07:16 PM