- Home /
all objects sharing script get destroyed?
So I have a script that spawns an object, the object waits for a bit and destroys itself. I also wanted the functionality to click on the object, and have it destroy itself.
If I just have one, it works beautifully.
If I have more then one of these "spawners" running, and just let them wait out their lives, they each die at their unique wait time.
However, if I click on one to destroy it, all of them get destroyed?
#pragma strict
var Countdown: int = 5;
var visible: float = 0.50;
var invisible: float = 0.50;
var blinkFor: float = 5.0;
var startState: boolean = true;
var endState: boolean = true;
var rayDistance: float = 0;
function Start ()
{
animation.Play ();
transform.position.z = -5;
renderer.enabled = startState;
yield WaitForSeconds(Countdown);
var whenAreWeDone = Time.time + blinkFor;
while (Time.time < whenAreWeDone)
{
if ( startState )
{
renderer.enabled = false;
yield WaitForSeconds(invisible);
renderer.enabled = true;
yield WaitForSeconds(visible);
}
else
{
renderer.enabled = true;
yield WaitForSeconds(visible);
renderer.enabled = false;
yield WaitForSeconds(invisible);
}
}
renderer.enabled = endState;
Destroy ( gameObject );
}
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
var hit :RaycastHit;
var ray :Ray = Camera.main.ScreenPointToRay(Input.mousePosition); // get mouse position
if (Physics.Raycast (ray, hit, rayDistance))
{
if (hit.transform.tag == "resource")
{
Destroy ( gameObject );
}
}
}
}
Thanks in advance for any help you can offer.
Answer by ICHeeryI · Sep 03, 2013 at 12:15 PM
Here is code:
//Destroy object on click
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if ( Physics.Raycast ( ray, hit, 100 )) {
if ( hit.collider.gameObject.renderer && Input.GetMouseButtonUp(0)){
//When user click left mouse button -select block-
Debug.Log(hit.collider.gameObject);
And here is destroy part:
Destroy (hit.collider.gameobject);
Hope it works!~
This worked perfectly! And I understand now why it was killing all of them! (just had to change the Destroy line to a capital "O");
Destroy (hit.collider.gameObject);
Thanks for the quick help! :)
Answer by robhuhn · Sep 03, 2013 at 12:15 PM
I guess because all of them have the tag "resource". You should check if this is the clicked object or destroy the object which was hit.
(code not tested)
if (hit.gameObject.Equals(gameObject))
{
Destroy(gameObject);
}
or
if (hit.transform.tag == "resource")
{
Destroy ( hit.gameObject );
}
Answer by DeveshPandey · Sep 03, 2013 at 12:16 PM
Add one more condition like-
if (hit.transform.tag == "resource" && hit.gameObject == gameObject)
{
Destroy ( gameObject );
}
Your answer
Follow this Question
Related Questions
Enemy health script using raycast not destroying tagged object 1 Answer
Javascript. Respawning a character after destroyobject(). 0 Answers
Following Object can't find newly Instantiated Object (Solved) 1 Answer
How to stop Unity from destroying prefab original when destroying copied instances? 2 Answers
Difference between Destroy and Destroy immediate (In this case). 2 Answers