- Home /
Detect if colliding with nothing
I would like a object called Polly to spawn, then when it is destroyed another one spawns. Hears the problem, every thing works but once I add the (else if) it starts giving me a whole bunch of errors. Also it dose not debug "Spawn Check". The object it spawns on is an empty object with a box collider. I was hoping that once the cube spawns it collides with the box collider it debugs "spawn check", then once destoryed another spawns Thanks
var polly : GameObject[];
function Start ()
{
var create : GameObject=
Instantiate(polly[0], transform.position, transform.rotation);
}
function Update () {
}
function OnCollisionEnter(Collided : Collision)
{
if(Collided.gameObject.name == "Enemy")
{
Debug.Log("Spawn Check");
else if()
{
var create : GameObject =
Instantiate(polly[0], transform.position, transform.rotation);
}
}
}
generally, never EVER use "else if". rewrite to not use "else if"
use a format like this where you break away (return) once done.
if (... collided .. )
{
whatever..
return;
}
Then how could I accomplish my goal without using else if, and what do you mean by return
I'm not sure if it was the formatting but you appear to have no condition in your else if. Also according to my count of the braces the else if isn't connected to the if, it's just hanging incorrectly (you don't appear to close the previous "ifs" brace before the else).
return returns from (exits) a function. @Fattie is suggesting that reading chains of else if statements is error prone and that it is clearer to exit the function when you are done, rather than using else if. This is good advice to beginners because it makes the code more obvious.
Your answer

Follow this Question
Related Questions
Making the collider change after being instantiate 2 Answers
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer
Destroy() not working on collision 2 Answers
PhysX and multiple instances at same location 1 Answer
Hi! How can I use the OnTriggerEnter2D function for the game objects that I instantiate? 3 Answers