- Home /
Load scene when colliding on GameObject
I've been looking for a few tutorials and other Unity Learn documentations, I couldn't find what I was exactly looking for. My script doesn't have errors, but it doesn't work at all.
So, I have my character ready, a few GameObjects and a "Game Over" scene. Whenever I collide with red sphere (otherwise called, "GravityBall_Red"), I should be in the "Game Over" scene (called "LevelRetry"). Now here's the script:
function OnCollisionEnter(other : Collision){
if(other.gameObject.name == "GravityBall_Red")
Application.LoadLevel ("LevelRetry");
}
If there's anything else you guys should need, I'd be good to give you a little more info.
Thanks
Answer by nicolasjr · Apr 12, 2014 at 09:30 PM
Hi Crush,
So, based on your code, I can only say that the problem is that you're not even detecting the collision, for some reason. Are you sure your condition is being reached? I mean, if you debug inside the if condition, it'd be reached?
There are some details about collisions you should pay attention to, but the most important is that the gameobject from the script you're verifying collision must have Rigidbody component.
Can you check that?
About the RigidBody, I've tried adding one but it didn't make a difference. I'm not getting info on the Console Log, although you're saying I should add more info to the script, like tell the script when the player collides with the GameObject?
Yes, I'm telling you to create a Debug.Log() inside the if statement that verifies the load of the other level. This way you'll know if the collision is being detected or not by unity!
Alright, still not getting anything from the log. I guess you're right, the condidtion doesn't reach and doesn't detect it.
Exactly. So, you've got to check the collisions.
Go from the basic:
The object that detects collision needs rigidbody;
The other object needs collider;
Is the other object really with the same name you're verifying? ;
etc....
Sometimes the problem is where you less expect.
Got it! $$anonymous$$y tag had a space at the end, finally works.
Thanks for your help :)