Restart on Collision

I am making a game where I want my scene named “GameScene” to restart when “Frog” (First Person Controller) hits “Water”. I am getting many errors and yes, I have the scene built. What can I do to fix this? What rigidbody’s or colliders do I add to the Frog or Water?

This is my code:

function Start () {

}

function Update () {

}

function OnCollisionEnter (collision : Collision)
{
if(collision.gameObject.tag == "Enemy")
    {
    Application.LoadLevel("GameScene");
    }
}

Its is not working…

What is wrong with it?

If you are using Unity Physics for collisions, then you can do these :

  • Put a Collider and a Rigid Body component on the frog.
  • Put a Collider and a Rigid Body component on the water.

After you do this, you can print a Debug.Log to see if OnCollisionEnter() is called.
If its called, then there is definitely something wrong with the if() clause.

Hey, try and use the code tags, please? It only helps the rest of us understand your scripting

Done! Thanks I didn’t know I could do that. I hope you could read it better ;D

I added a Debug.Log to say “Water Entered” in the if statement and it wasn’t called, so what is wrong with my if statement??

if you put the Debug.Log outside of the “if” does it print messages to the console.

Yes it does.

Someone said I should try OnTriggerEnter since i’m using a first person controller but it still isnt working…

Since you are seeing the debug.log outside of the if, the collision reporting seems to be working. Have you set the Tag at the top of the inspector on your frog and on your water to “Enemy”? Not knowing how you have the game objects setup, I could only guess which one should have the tag set, but setting both should get you through the “if”.

Yeah, i could read it regardless. It just helps keep things organized and simple.

Well, the debug doesnt work inside the function OnCollisionEnter, I thought that you meant under Function Start. I don’t think the function works at all. Does it need to be a OnTriggerEnter because its a character controller? I dont know what is happening.

Frog: Imgur: The magic of the Internet
Water: Imgur: The magic of the Internet

Ok, yes, you need to use OnTriggerEnter because your colliders have IsTrigger checked. See if you can get a debug.log to fire in that, and if not, post your latest code with OnTriggerEnter.

Just noticed that both colliders are marked as kinematic, I don’t believe you will get callbacks on 2 kinematic bodies colliding.

EDIT: Nevermind, it looks like triggers should send a message for kinematic bodies, so that shouldn’t be the problem.

OnTriggerEnter Code:

Function OnTriggerEnter (other : Collider)
{
Debug.Log("Zone Entered");
if(gameObject.tag == "Enemy")
    {
    Application.LoadLevel("GameScene");
    }
}

Only thing I see wrong there is that you should check other.gameobject.tag == “Enemy”. I assume you are not seeing “Zone entered” in the output log?

If not, I think I would need to see if you could zip up the project and upload it so I can see if i can figure anything out by poking around in the project.

ITS WORKING!

Thank you so much for being patient, I am new and that was difficult for me.

Excellent! Was the solution something mentioned above? Just want to know so others having similar problems can see what resolved yours.

Glad I could help!

When I added other to OnTriggerEnter it started to work.

How would I make a specific radius? For my water I had to make 50 copies so that my sphere radius could fit well.