- Home /
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);
}
}
}
}
}
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
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.
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
@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
Ahh that is realy quite simple I will update the script shorlty
Thank you for your patience, it works just the way i'd like it. I appreciate the help
Answer by tanoshimi · Jul 20, 2014 at 12:32 PM
As a quick fix, try replacing:
Destroy (gameObject);
with:
Destroy (hitInfo.transform.gameObject);