Other
Game Object not being destroyed at 0 lives
I'm trying to get it so that the player game object will destroy itself when lives = 0 which is done by hitting a prefab 3 times called shuriken.
Here is the code I'm using: public class PlayerMovement : MonoBehaviour {
public float speed;
private Rigidbody2D rb2d;
private int lives=3;
public GameObject Player;
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("shuriken"))
{
lives-- ;
if (lives <= 0)
{
Destroy (Player);
}
}
}
Do you assign "GameObject Player" correctly? Have you tried simply using Destroy(gameObject)
(which should destroy the GameObject this script is assigned to)? Have you tried Debug.logError("Collision detected!")
to see if there even is a collision occuring?
I have tried the debug logError and it is detecting the collision between the player and shuriken, however the Destroy(gameObject) didnt work in place of "Destroy (Player)"
@plumel very strange. I guess you have tried debugging every step? (as in, Debug.logError(other.gameObject.tag), Debug.logError(lives), Debug.logError(Player.tag))
If not you should do that to deter$$anonymous$$e exactly where it won't run. $$anonymous$$aybe the reference to the gameObject is wrong (doesn't seem like it though, sine Destroy(gameObject) won't work. What seems most likely is that you have a typo in the Tag. Do the shuriken have the Tag "shuriken" (notice all lower case letters)? Do the shuriken have a tag at all? Have you tried comparing the gameObject.name ins$$anonymous$$d?
I should think the FindGameObjectsWithTag should work, here's the link if you want to look as well http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html
I have made a public variable "Player" and then made a Player prefab from the gameobject, as unity wouldn't allow me to destroy the actual GameObject. I have dragged the prefab into the public variable on the script but still nothing..
I don't know what I've done!
where is this script attached? is it attached to the player object or something else?
@plumel I will copy my previous reply: I guess you have tried debugging every step? (as in, Debug.logError(other.gameObject.tag), Debug.logError(lives), Debug.logError(Player.tag))
If not you should do that to deter$$anonymous$$e exactly where it won't run. $$anonymous$$aybe the reference to the gameObject is wrong (doesn't seem like it though, sine Destroy(gameObject) won't work. What seems most likely is that you have a typo in the Tag. Do the shuriken have the Tag "shuriken" (notice all lower case letters)? Do the shuriken have a tag at all? Have you tried comparing the gameObject.name ins$$anonymous$$d?
As it is now we don't even know if your script even tries to call Destroy(), does it even get to that line? How do we know it doesn't get stuck on the if-statement because of a typo?
I'm now getting "Cannot use local variable 'lives' before it is declared. The declaration of this local variable hides the field 'Player$$anonymous$$ovement.lives'.
This is on the line 'lives--l'
Answer by Saif_youssef89 · Mar 08, 2016 at 01:36 PM
If this script is a component of "player" then there is no need to have Destroy(player)
try:
destroy();
In any case, add a Debug.Log("Trigger Entered") or something to understand where the code is failin. Good luck and keep us updated!
I have tried Destroy(); ins$$anonymous$$d of the Destroy(Player);
Unfortunately, I have an error saying "No overload for method 'Destroy' takes 0 arguments.
Thanks for your help
Ok, how about we try to destroy the player using tags, assign a tag for the player gameobject and then use:
Destroy(GameObject.FindGameObjectsWithTag("Player");
Follow this Question
Related Questions
Destroying instantiated game objects from prefab when coliding with the ground 1 Answer
How to destroy GameObject if it overlaps another GameObject 2 Answers
Can't destroy gameobject inside coroutine? 0 Answers
My OnTriggerEnter destruction script won't trigger 1 Answer
Why Original GameObject Destroy Clones ? 0 Answers