- Home /
Destroying gameobject after its been cloned
I know that the script is accessing it after every clone, but how am i meant to delete a asset that is being used in the script? I dont want it in the scene after a few seconds. (player has to dodge them)
Thanks
var theObject:GameObject;
var maxPosx:float = 2.898618; // x axis
var minPosx:float = -1.89557; // x axis
var delay:float = 0.5; // delay each spawned item by 0.5
var maxPosy:float = 5.086665; // y axis
var minPosy:float = 5.086665; // y axis
var lifeTime = 1.0;
function Start(){
StartCoroutine(spawn());
}
function spawn() : IEnumerator {
for (;;) {
yield WaitForSeconds(delay);
var theNewPos= new Vector3 (Random.Range(minPosx,maxPosx),Random.Range(minPosy,maxPosy),0);
var go : GameObject = Instantiate(theObject);
go.transform.position = theNewPos;
Destroy(theObject, lifeTime);
}
}
delete the things that spawn?
By the way, in UnityScript I don't believe you have to do "StartCoroutine", any function in UnityScript with a yield is a coroutine.
yield works fine. the whole script works, but if i destroy a spawned item it flags an error because the script is still accessing it.
this is more of a question than an answer, but what if ins$$anonymous$$d of destroying theObject, you destroyed go? Because I think you are destroying the original gameObject when you do that, ins$$anonymous$$d of the clone.
aaaand that worked... i need some sleep. Seriously cannot believe i missed that. I knew go was the clone, im a shape. Thanks again
Answer by corriedotdev · Mar 24, 2014 at 07:45 PM
Answer from Destran, i needed to destroy 'go' instead of the gameobject. This i knew but lack of sleep does stupid things to people. Thanks