Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Restart on Collision

Discussion in 'Scripting' started by iEntity, Feb 11, 2015.

  1. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    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:


    Code (JavaScript):
    1.  
    2. function Start () {
    3.  
    4. }
    5.  
    6. function Update () {
    7.  
    8. }
    9.  
    10. function OnCollisionEnter (collision : Collision)
    11. {
    12. if(collision.gameObject.tag == "Enemy")
    13.     {
    14.     Application.LoadLevel("GameScene");
    15.     }
    16. }
    17.  

    Its is not working..

    What is wrong with it?
     
    Last edited: Feb 11, 2015
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    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.
     
  3. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Hey, try and use the code tags, please? It only helps the rest of us understand your scripting
     
  4. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    Done! Thanks I didn't know I could do that. I hope you could read it better ;D
     
  5. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    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??
     
  6. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    if you put the Debug.Log outside of the "if" does it print messages to the console.
     
  7. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    Yes it does.
     
  8. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    Someone said I should try OnTriggerEnter since i'm using a first person controller but it still isnt working..
     
  9. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    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".
     
  10. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Yeah, i could read it regardless. It just helps keep things organized and simple.
     
  11. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    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.
     
  12. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
  13. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    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.
     
  14. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    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.
    http://docs.unity3d.com/Manual/CollidersOverview.html
     
    Last edited: Feb 12, 2015
  15. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    OnTriggerEnter Code:

    Code (JavaScript):
    1. Function OnTriggerEnter (other : Collider)
    2. {
    3. Debug.Log("Zone Entered");
    4. if(gameObject.tag == "Enemy")
    5.     {
    6.     Application.LoadLevel("GameScene");
    7.     }
    8. }
     
  16. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    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.
     
  17. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    ITS WORKING!

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

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    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!
     
  19. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    When I added other to OnTriggerEnter it started to work.
     
  20. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    How would I make a specific radius? For my water I had to make 50 copies so that my sphere radius could fit well.