• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Feb 15, 2018 at 01:45 PM by meat5000 for the following reason:

Other

avatar image
0
Question by plumel · Mar 08, 2016 at 12:21 PM · prefabdestroydestroygameobject

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);
         }
     }
 }
Comment
Add comment · Show 14
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Salmjak · Mar 08, 2016 at 12:31 PM 0
Share

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?

avatar image plumel Salmjak · Mar 08, 2016 at 01:30 PM 0
Share

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)"

Show more comments
avatar image plumel · Mar 08, 2016 at 02:50 PM 0
Share

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!

avatar image Saif_youssef89 plumel · Mar 08, 2016 at 02:52 PM 0
Share

where is this script attached? is it attached to the player object or something else?

Show more comments
avatar image Salmjak plumel · Mar 08, 2016 at 03:19 PM 0
Share

@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?

Show more comments

1 Reply

  • Sort: 
avatar image
-1

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!

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image plumel · Mar 08, 2016 at 01:43 PM 0
Share

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

avatar image Saif_youssef89 plumel · Mar 08, 2016 at 01:46 PM 0
Share

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");

avatar image plumel Saif_youssef89 · Mar 08, 2016 at 02:23 PM 0
Share

Sorry for the late reply, alt text

issue2.png (15.3 kB)
Show more comments
Show more comments

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying instantiated game objects from prefab when coliding with the ground 1 Answer

Destroying prefabs breaking scripting for other prefabs 0 Answers

Objects getting destroyed in unity test, but only sometimes in the phone app 0 Answers

A step vanishes when you step off it? 1 Answer

How do you make a destroy command with timer and with tag. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges