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();
}
}
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.
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.
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();
}
}
}
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?
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.
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.
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.