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.