Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
1
Question by Reefer · Jul 20, 2013 at 01:02 AM · c#gameobjectplayerdelete

[SOLVED] How I can detect deleted gameobject

How I can detect when particular gameobject with tag Player gets deleted so I can show an game over message? Please help in c# as I'm learning that.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by clunk47 · Jul 20, 2013 at 02:42 AM

EDIT: Here is the working version of the script. Edited and tested, working fine for me.

 using UnityEngine;
 using System.Collections;
  
 public class Valikko : MonoBehaviour 
 {
     GameObject Pallo;
     int bricksLeft;
     bool destroyedAllBricks = false;
     
     void Start () 
     {
         Pallo = GameObject.FindGameObjectWithTag("Player");
     }
  
     void Update () 
     {
         GameObject[] bricks = GameObject.FindGameObjectsWithTag("Brick");
         bricksLeft = bricks.Length;
         

             if(Input.GetKeyDown(KeyCode.A))
     {
      if(bricks.Length > 0)
      Destroy (bricks[bricks.Length - 1]);
     }
     if(bricksLeft <= 0)
     {
       destroyedAllBricks = true;
     }

     }
     
     void OnGUI () 
     {
         if(Pallo == null)
         {
             GUI.Box (new Rect (0,60,200,25),"Game Over!");
         }
        
         if(destroyedAllBricks)
         {
             GUI.Box (new Rect (0,30,200,25),"All bricks destroyed!");
         }
         
         else
         {
             GUI.Box (new Rect(0,30,200,25),"Bricks remaining :" + bricksLeft);
             GUI.Box (new Rect (0,0,200,25), "CrazyBricks 3D v0.2");
             GUI.Box (new Rect (Screen.width -300,0,300,25), "Broken Signal Entertainment (c) 2013");
         }
     }   
 }
Comment
Add comment · Show 16 · 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 clunk47 · Jul 20, 2013 at 03:03 AM 1
Share

Remove the semicolon after if(Pallo == null); You shouldn't have an if statement like if(this);{}, has to be if(this){}. I can tell it's a simple type-o because your other if statements are fine.

avatar image clunk47 · Jul 20, 2013 at 03:42 AM 1
Share

Ok I took the script and made some $$anonymous$$or adjustments. Working fine on my end. I used an object tagged "Player" and some cubes tagged "Brick" everything works fine. I edited my answer with the new version of the script. Hope it works on your end now.

avatar image clunk47 · Jul 20, 2013 at 03:47 AM 1
Share

The problem is, you actually need to delete the bricks to change the count since the count will always be the actual amount of bricks in the scene. Hitting a key and saying bricksLeft-- won't work. Don't maximize the scene on runtime, and delete each brick from the hierarchy, your count will subtract.

avatar image clunk47 · Jul 20, 2013 at 04:17 PM 2
Share

$$anonymous$$ake sure there is nothing else tagged as Player. Once player is destroyed, this should print ("player does not exist!". This tells us something is causing the player not to be null. I am getting game over message once player object is destroyed when I try this script on my machine. In what way are you destroying player? Are you doing with another script? When I test I delete player from hierarchy when game is running, and get game over message. Try this to see if player is simply being destroyed or not.

avatar image Reefer · Jul 20, 2013 at 10:05 PM 2
Share

Ahh, it was because I was only setting that object inactive when it hit the ground. Now when I changed it to Destroy command it works just fine. Thank you for all the trouble you went through while trying to solve this problem which was so stupid and small that I should had known this..

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Issue with GameObject has been destroyed but it is not destroyed 1 Answer

Multiple Cars not working 1 Answer

How to determine the direction of an object to the player. 1 Answer

deleting self-gameobject,deleting self-gameobject 0 Answers

Having trouble swapping GameObjects on button click 1 Answer


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