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

Collsion less

Discussion in 'Scripting' started by ThuverX, Oct 31, 2015.

  1. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    I have a collision script and when it hits you need to get -1 of the life score; It works but instead of doing 1 it does it 20 times. I want it to go only 1 time
    Lives script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class lives : MonoBehaviour {
    5.     public float Life = 3;
    6.     public GUIText livesText;
    7.     public bool Dead = false;
    8.  
    9.     void Start()
    10.     {
    11.         livesText.text = "Lives left:" + Life;
    12.     }
    13.  
    14.     public void Lives()
    15.     {
    16.         Life -= 1;
    17.         livesText.text = "Lives left:" + Life;
    18.     }
    19.     void Update()
    20.     {
    21.         if (Life <= 0)
    22.         {
    23.             Dead = true;
    24.         }
    25.     }
    26. }
    27.  
    Collisions script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class Collisions : MonoBehaviour {
    5.     public AudioClip snd;
    6.     public lives livesScript;
    7.  
    8.     void OnCollisionEnter(Collision other) {
    9.         Debug.Log("<color=blue>Info:</color> Player Collision Called");
    10.         livesScript.Lives();
    11.     }
    12. }
     
    Last edited: Oct 31, 2015
  2. MShibli

    MShibli

    Joined:
    Jun 15, 2015
    Posts:
    2
    What does your scene hierarchy look like? can you send me your scene? I can have a look at it
     
  3. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    You mean the .unity file
    or the project
     
  4. McMayhem

    McMayhem

    Joined:
    Aug 24, 2011
    Posts:
    443
    It looks like your collision script doesn't do any checks to make sure the object it's colliding with is the one it needs to collide with. So if the object that script is attached to intersects with any other colliders either at start or at any other point, the -1 Life code will get called each time it happens. You might want to do a check for tag.
     
    MShibli likes this.
  5. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    So you only see the debug message once when the collision happens, but the life count gets decremented twenty times?

    Edit: Oops too slow, I believe McMayhem has your answer.
     
  6. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    No it also does the message the same amount (10-30)
     
  7. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    I did what you said but it still counts to many.
    I changed the code to:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class Collisions : MonoBehaviour {
    5.     public AudioClip snd;
    6.     public lives livesScript;
    7.  
    8.     void OnCollisionEnter(Collision col) {
    9.         if (col.gameObject.tag == "level")
    10.         {
    11.             Debug.Log("<color=blue>Info:</color> Player Collision Called");
    12.             livesScript.Lives();
    13.         }
    14.     }
    15. }
    I think it adds 1 every frame I am in the object. Because I have all constraints on. Because otherwise it just flops arround
     
  8. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Do you have some sort of wave of objects which move toward the collision area, similar to a tower defence game?
    Add a debug message outside the if structure which shows the name of the game object which triggered the collision, and let us know what happens.
     
  9. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    How would I do this? When I have this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [RequireComponent(typeof(AudioSource))]
    4. public class Collisions : MonoBehaviour {
    5.     public AudioClip snd;
    6.     public lives livesScript;
    7.  
    8.     void OnCollisionEnter(Collision col) {
    9.         Debug.Log("<color=blue>Info:</color> Player Collison with" + col);
    10.         if (col.gameObject.tag == "level")
    11.         {
    12.             livesScript.Lives();
    13.         }
    14.     }
    15. }
    It only puts out unityengine.collision
     
  10. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    This is no valid code:
    Code (CSharp):
    1.  
    2.     public lives livesScript;
    3.  
    4.     void OnCollisionEnter(Collision other) {
    5.        livesScript.Lives();
    6.     }
    7.  
    livesScript should be a null reference. So I guess there's some more code? Probably a start function to initialize the lives variable? You probably doing something in the rest of your code that leads to the described behavior?
     
  11. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    The code in my Thread is all of it.
     
  12. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Debug.Log( col.gameobject.name);
     
  13. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    No its not an tower defense but when I go right thru the object it counts all of the meshes it touches.

    But as I said I think it counts every frame so that's why it is so much.

    Edit: As you can see here:

    I go trough one part of the mesh but I counts 4 times.
     
  14. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Alright. Let's start from a scratch. :)
    You do have a single player gameobject and the scripts ('Lives' and 'Collisions') from your initial post are attached to this gameobject?
    And then you have several other gameobjects the player GO collides with.

    Correct?
     
  15. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    Collisions is on my player and lives is on my GuiText.
     
  16. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Imho there's no question that the code from your initial post won't work. You'll get a NullReferenceException with this code.
    Either the code is not your current code, or there's more code. Please check.
     
  17. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    He could be assigning the value through the inspector, which was what I assumed.

    I thought that could have been the issue, but since you're using OnCollisionEnter it should only happen once.

    What kind of collider is on the objects that collide with the object which has the Collision script, are you using mesh colliders?
     
    Last edited: Oct 31, 2015
  18. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Hm. Do I miss something? You can't assign a script component reference via the inspector.
     
  19. MShibli

    MShibli

    Joined:
    Jun 15, 2015
    Posts:
    2
  20. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Oh, you can't do that? I honestly didn't know since I always do things via script opposed to using the inspector.
     
  21. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    Yes I am
     
  22. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Hm. He stated that the scripts are assigned to different objects. But, whatever...I'll keep my mouth closed. :)
     
  23. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    Does the colliding object contain several children objects? I suspect that you might be getting multiple collisions due to child objects, based on the debug messages you've showed.

    Make sure there is only one collider, and perhaps try using a different collider type to see if anything changes.
     
    Last edited: Oct 31, 2015
  24. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    Thanks I will try that! :D
     
  25. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    I tried creating a cube. With only a box collider but it still did allot of outputs.
    Now I'm out of ideas...
     
  26. flonch

    flonch

    Joined:
    Aug 20, 2014
    Posts:
    63
    That's so weird. Could you show us the debug messages when you use the simplified cube-object?
    Also can you verify that the lives get decreased when the expected collision occurs or is it happening unexpectedly (i.e. when your load the scene)?
     
  27. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Try starting your player cube slightly above a new cube that has no tag. When you run it should fall onto the untangged cube & have no impact on the lives. If it does then thee is something else going on somewhere. If it doesn't, while still playing, move the player cube (I'm assuming you have a movement script on your player already) around so it falls off the untagged cube onto one of your tagged objects.

    This is just taking a step back & trying to ascertain that everything is still working properly before you have any collisions.
     
  28. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    It does the collisions on a collide so that's good.
    The debug messages:
     
  29. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    As far as I understand you, I have made a cube falling onto an other cube, the collision was called 1 time. But I think it is because I'm passing thru the object when using my player, instead of this cube that is just falling on top of it.
     
  30. ThuverX

    ThuverX

    Joined:
    May 25, 2015
    Posts:
    35
    I made an gif of it have a look at what settings I have in the inspector. And the console(the first thing in the console is the detection):

    This is just called normally, so I don't get why that isn't on my player.
    I'm now going to try using one object for all the colliders instead of all the meshes a custom collider.

    EDIT it worked, thanks to everybody helping me!! :D
    Using 1 collider and OnCollisionExit it worked!
     
    Last edited: Nov 2, 2015