Collsion less

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:

using UnityEngine;
using System.Collections;

public class lives : MonoBehaviour {
    public float Life = 3;
    public GUIText livesText;
    public bool Dead = false;

    void Start()
    {
        livesText.text = "Lives left:" + Life;
    }

    public void Lives()
    {
        Life -= 1;
        livesText.text = "Lives left:" + Life;
    }
    void Update()
    {
        if (Life <= 0)
        {
            Dead = true;
        }
    }
}

Collisions script:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Collisions : MonoBehaviour {
    public AudioClip snd;
    public lives livesScript;

    void OnCollisionEnter(Collision other) {
        Debug.Log("<color=blue>Info:</color> Player Collision Called");
        livesScript.Lives();
    }
}

What does your scene hierarchy look like? can you send me your scene? I can have a look at it

You mean the .unity file
or the project

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.

1 Like

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.

No it also does the message the same amount (10-30)

I did what you said but it still counts to many.
I changed the code to:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Collisions : MonoBehaviour {
    public AudioClip snd;
    public lives livesScript;

    void OnCollisionEnter(Collision col) {
        if (col.gameObject.tag == "level")
        {
            Debug.Log("<color=blue>Info:</color> Player Collision Called");
            livesScript.Lives();
        }
    }
}

I think it adds 1 every frame I am in the object. Because I have all constraints on. Because otherwise it just flops arround

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.

How would I do this? When I have this:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Collisions : MonoBehaviour {
    public AudioClip snd;
    public lives livesScript;

    void OnCollisionEnter(Collision col) {
        Debug.Log("<color=blue>Info:</color> Player Collison with" + col);
        if (col.gameObject.tag == "level")
        {
            livesScript.Lives();
        }
    }
}

It only puts out unityengine.collision

This is no valid code:

    public lives livesScript;

    void OnCollisionEnter(Collision other) {
       livesScript.Lives();
    }

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?

The code in my Thread is all of it.

Debug.Log( col.gameobject.name);

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.

Alright. Let’s start from a scratch. :slight_smile:
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?

Collisions is on my player and lives is on my GuiText.

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.

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?

Hm. Do I miss something? You can’t assign a script component reference via the inspector.

@Duugu , ‘lives’ and ‘collisions’ are both attached to the same game object and he’s dragging ‘lives’ into the inspector to get a reference.
@ThuverX have you checked all your tags? Make sure the object you want to collide with is the only with the tag “level”. Also check you physics settings and make sure the collisions are set up the way you want them.
http://docs.unity3d.com/Manual/class-PhysicsManager.htmlhttp://docs.unity3d.com/Manual/class-PhysicsManager.html

Oh, you can’t do that? I honestly didn’t know since I always do things via script opposed to using the inspector.