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
0
Question by RandomUser123 · Jul 20, 2014 at 12:27 PM · destroytag

Destroying gameObject destroys all objects

I would like to destroy a gameObject in my scene after so conditions are met, this works but it also destroys the other objects that have the same script even though their conditions have not met yet, hopefully the code explains it a bit:

 using UnityEngine;
 using System.Collections;
 public class selectObject : MonoBehaviour 
 {
 
     public bool isSelected = false;
 
     public GameObject[] prefab;
 
     public string ballColor = "";
  
     void Update () 
     {
 
         if (Input.GetMouseButtonDown (0))
         {
             RaycastHit hitInfo = new RaycastHit ();
             bool hit = Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
 
             if (Input.GetMouseButtonDown (0) && hit) 
             {
                 Debug.Log("Hit " + hitInfo.transform.gameObject.name);
                 if (hitInfo.transform.gameObject.tag == "redBall")
                 {
                     isSelected = true;
                     ballColor = "red";
                 }
 
                 else if(hitInfo.transform.gameObject.tag == "blueBall")
                 {
                     isSelected = true;
                     ballColor = "blue";
                 }
                 else if(hitInfo.transform.gameObject.tag == "greenBall")
                 {
                     isSelected = true;
                     ballColor = "green";
                 }
                 else if(hitInfo.transform.gameObject.tag == "yellowBall")
                 {
                     isSelected = true;
                     ballColor = "yellow";
                 }
 
 
                 if(isSelected == true && ballColor == "blue")
                 {
                     if (hitInfo.transform.gameObject.tag == "bCapsule")
                     {
                             //It gets here but i only want the blue ball destroyed, it destroys all balls in the scene right now
                             Debug.Log ("I'm here");
                             Destroy (gameObject);
                         
                     }
     
                 }
             }
         }
     
     }



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

2 Replies

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

Answer by deltamish · Jul 20, 2014 at 12:30 PM

Hi,

Thats really easy problem The thing is you are Destroying the gameObject on which the script resides instead of destroying the one that is hit by the ray

 public GameObject SelectedBall ; // Made it public so that you could see wether its is working or not
 
  void Update () 
     {
  
         if (Input.GetMouseButtonDown (0))
         {
             RaycastHit hitInfo = new RaycastHit ();
             bool hit = Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
  
             if (Input.GetMouseButtonDown (0) && hit) 
             {
                 Debug.Log("Hit " + hitInfo.transform.gameObject.name);
                 if (hitInfo.transform.gameObject.tag == "redBall")
                 {
 
                     isSelected = true;
                     ballColor = "red";
                    SelectedBall = hitInfo.transform.gameObject;
                 }
  
                 else if(hitInfo.transform.gameObject.tag == "blueBall")
                 {
                     isSelected = true;
                     ballColor = "blue";
  SelectedBall = hitInfo.transform.gameObject;
                 }
                 else if(hitInfo.transform.gameObject.tag == "greenBall")
                 {
                     isSelected = true;
                     ballColor = "green";
  SelectedBall = hitInfo.transform.gameObject;
                 }
                 else if(hitInfo.transform.gameObject.tag == "yellowBall")
                 {
                     isSelected = true;
                     ballColor = "yellow";
  SelectedBall = hitInfo.transform.gameObject;
                 }
  
  
                 if(isSelected == true && ballColor == "blue")
                 {
                     if (hitInfo.transform.gameObject.tag == "bCapsule")
                     {
 // Here the object (ball) is destroyed only if the object has name blue
                             Debug.Log ("I'm here");
                             Destroy (SelectedBall);
  
                     } // Similarlry
                     
                 }
             }
         }
  
     }

Destroy(gameObject) destroys the object on which this script resides

so use Destroy(hitinfo.transform.gameobject)

Edit The Object should contain either of the tags above inorder for them to be destroyed

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 RandomUser123 · Jul 20, 2014 at 12:39 PM 0
Share

Hi, thanks for the response, it certainly makes a difference, the thing is, it destroys the capsule i click on rather than the ball, would you know why?

Edit: Sorry i'll make it more clear, I have 4 different coloured balls that keep spawning on screen randomly, my previous problem with Destroy(gameObject) was that it destroyed all balls on screen whereas i just want it to destroy the one i had clicked on.

As of now, the balls arent' destroyed but the capsule is, which i need to keep in the scene.

avatar image deltamish · Jul 20, 2014 at 01:01 PM 0
Share

@RandomUser123

It is destroying only the capsule because we are saying it to check for capsule and destroy only that

I have edited the answer try it out

avatar image JayFitz91 · Jul 20, 2014 at 01:22 PM 0
Share

@deltamish sorry for the questions, but your response has not resolved my issue unfortunately, The aim is like this:

-The scene starts with 4 different coloured capsules

-Every 2 seconds a random coloured ball appears on screen

  • would like to click on a ball, store it in a variable isSelected, then click on a capsule, then if the two colours match, destroy the ball and increase score

At the moment, the ball is destroyed as soon as its clicked or the capsules are destroyed.

Sorry again about this, but i feel i'm quite close and it's bugging me

avatar image deltamish · Jul 20, 2014 at 01:23 PM 0
Share

Ahh that is realy quite simple I will update the script shorlty

avatar image RandomUser123 · Jul 20, 2014 at 01:41 PM 0
Share

@deltamish

Thank you for your patience, it works just the way i'd like it. I appreciate the help

Show more comments
avatar image
0

Answer by tanoshimi · Jul 20, 2014 at 12:32 PM

As a quick fix, try replacing:

 Destroy (gameObject);

with:

 Destroy (hitInfo.transform.gameObject);
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

When all objects with a certain tag has been destroyed, load the next level! 3 Answers

The object of type 'Transform' has been destroyed but you are still trying to access it. 1 Answer

Perform a Task when Objects are Destroyed 4 Answers

Key Press to Trigger animation when within range 1 Answer

Destroy a gameobject created with Instantiate? 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