Search Unity

GameOver Scene on Collisions

Discussion in 'Scripting' started by Jana1108, Jul 1, 2015.

  1. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Hi, so in my game I want the 'GameOver' scene to load when a cube collides with one of my many 'enemys' which are really just cylinders. I have tried various ways and none of them seem to work. I prefer C# but any coding language will be fine if it works! Please help I want to finish my game!
    Also do I put the script on just the 'players' settings or do I need to drag the code into the enemy's preferences as well?

    Thanks in advance! :)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Basically you want to use OnCollisionEnter function in your player script and check if the object you hit is an enemy and then load the next scene.

    I could just write you a simple script but I suggest you try finding the solution yourself, just look up OnCollisionEnter and see some script examples.

    And if your game happens to be 2D, which I doubt, since you use cylinders, you may want to check out OnCollisionEnter2D.
     
  4. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Thanks for your reply,
    Yes I have looked at those but I still can't seem to get it working... anymore advice?
     
  5. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Thanks for your reply :)
    I have tried all of this but I still can't get it working. Any other ideas?
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if you have tried something and it doesn't work can you post your code so we can see why it's not working rather than guessing? use [code ][/code ] tags when you paste it in.
     
  7. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Hi LeftyRighty,
    I have tried and deleted many different samples of code so unfortunately I cannot copy and paste them. There are no errors in the console when I write the code but nothing seems to happen when I play my game. I have tried different techniques in C# and Javascript such as the OnCollisionEnter, Trigger events etc but they don't seem to work.
     
  8. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Also I was wondering, when I have written my code, I place add it as a component to my player (cube), do I need to add it as a component to the cylinders also?
     
  9. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Are you sure your both objects had colliders and that atleast one of them had a rigidbody? And that you actually attached the script to your object. Also debugging may help to find a solution to see if it even is called.

    Edit: only one of your objects requires the script as component, if you check for collides with enemies, attach it to your player.
     
  10. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Yes, they both have rigidbodys and one has a box collider and one has a capsule collider.
     
  11. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Can someone please just walk me through STEP BY STEP how to do it, including what I need to add the code to as a new component etc...? It would be much appreciated as I am nearly finished my game and I think it has a lot of potential.
     
  12. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Attach a script like this to you player object. You'll need to change the specifics for your project as we have no code to reference.

    Code (CSharp):
    1. void OnCollisionEnter(Collision collision)
    2. {
    3.     if(collision.gameObject.tag.Equals("enemy"))
    4.     {
    5.          Application.LoadLevel("Game Over");
    6.     }
    7. }
     
  13. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Writing this without access so unity, so I may forget some stuff and there may be some errors.

    1. Add a Player object and assign a rigidbody and a collider of your choise to it.
    2. Add an Enemy object and assign a rigidbody and a collider of your choise to it. (and optional, add "Enemy" tag to it)
    3. Create a new C# script and add it as a component to Player (or use any script attached to player, no need to create a new one if you already have one)
    4. Add this in your script (may contain some errors, can't test)
    Code (CSharp):
    1.  
    2. void OnCollisionEnter(Collision coll){
    3.            Debug.Log("Collision"); //Check to see if it even registers a collision, if it works you can remove this line
    4.            if(coll.gameobject.tag == "Enemy"){ //sees if the object your player collided with has a tag called "Enemy", this can be replaced with if(coll.gameobject.name == ... but using a tag is an easy way to do it.
    5.                     //Add your code here, like:
    6.                     Application.LoadLevel("GameOver");    //Replace "GameOver" with your scene you want to load
    7.                     Debug.Log("Working");  //if working, you can remove this line.
    8.            }
    9. }
    10.  
    5. Make sure your scene has been added in build settings (can't remember where exactly)
    6. Cause a collision between player and enemy and see if it works.

    Hopefully I didn't forget anything but since i'm writing this off the top of my head I may have forgotten something or made some script errors.

    No offense tho, but how have you managed to create a game without being able to do stuff like this? Anyways good luck

    Edit: it seems that Timelog posted it a second before me, well now you got 2 examples
     
  14. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    W
    I tried this and it doesn't seem to print anything in the console or load up the new scene.
    Also about your comment on how was I able to make a game like this if I don't know the basics, well I do know some basics but I'm only 14 years old so I have been doing a lot of guess and check and research as this is my first ever unity game.
     
  15. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Didn't work :(
    Am I meant to have the player or enemy set on a specific collision detection in the inspector? E.g. Discrete, continuous etc...
     
  16. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Didn't mean to offend you, you're just a beginner you'll get better. I remember the time when I had trouble with these things :D

    I created a quick project file for you and attached it in this message. Open it in Unity and click play, "Enemy" will fall on "Player" and there are some debug messages. If it works for you (works for me) then just learn from it and do the same thing in your project.

    I'm not sure if Unity has some rules about uploading files and I had to put it in a rar file because i'm not sure if you can upload folders. The only thing it contains is the unity project file, but scanning the file for any possible viruses is recommended when downloading files from the internet. :D

    If this doesn't work for you I recommend downloading the latest version of Unity.I used version 5.0.2f1.
    Good luck.

    Edit: If there's any chance your game is 2D, remember to use 2D colliders and OnCollisionEnter2D
     

    Attached Files:

    Last edited: Jul 2, 2015
  17. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    I can't open the file... are you able to send me the unity file instead?
     
  18. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    I repacked the file in a .zip format, any windows OS should be able to use that.
     

    Attached Files:

  19. Jana1108

    Jana1108

    Joined:
    Jun 27, 2015
    Posts:
    215
    Thanks I'll check that out now! Also I've started a new game while finishing the finals of this one... I seem to be able to get the collisions working in the other one even using the methods that failed for me on this project. Maybe I adjusted a setting that I shouldn't have? Thanks for all the help :)