|
Hi :) So I have an object. When it is created, it also creates a second one in the same position (which are particles). When the player hits the object, this object is destroyed. That's the script here, which is working. Except that I can't destroy the particle object created in the function Start....
So my error is the line with :
If anyone could help me, that would be very nice....
(comments are locked)
|
|
The problem is that the "clone" variable is declared in Start, so it only exists in Start, and not in OnTriggerEnter. You have to make "clone" be a global variable instead of a local variable. var prefab : GameObject; var clone : GameObject; function OnTriggerEnter(collisionInfo : Collider){ Destroy (clone); Destroy (gameObject); } Does'nt work too .... :(
Mar 24 '10 at 07:31 PM
CyberTwister
(comments are locked)
|
|
You're instantiating a prefab called "Clone" then later trying to Destroy "Clone". Problem is I THINK that what you need to do is to destroy the Clone(Clone) (as appears in Inspector when you play) Make sure the prefab has a tag - e.g. name the tag "Clone". Then have: Destroy(GameObject.FindWithTag("Clone")}; Get rid of the Destroy (clone); Destroy (gameObject) stuff you have etc and just have the above - see if that works?? Bad idea - there could be many objects with that tag. The OP is close, just needs to follow the other Answers and make the variable non-local.
Jun 21 '10 at 06:11 PM
Cyclops
(comments are locked)
|
|
Your problem is called variable scoping (what Eric5h5 said, and I voted up his answer for that). To clarify:
So all you need to do is this:
(comments are locked)
|
|
Eric5h5 is correct. You are declaring the variable twice by saying var clone= both as a statement and in the function. In the function just say clone = not var clone = .
(comments are locked)
|
|
I'm late but.... I've found a way to Do this and works for me. I attach one Script in the prefab so when i Instanced this in other Variable.... i call to their self destruction function. obviously declare the variable global var instant = null; var pref : GameObject; //load in inspector , it has the script with the destroy function function Start(){ { instant=Instantiate(pref,Vector3(0,2,0), Quaternion.identity); instant.selfDestroy();//in this example is created and destroy inmediatly } function myDestroy(){//this is call when i need destroy instant { instant.selfDestroy(); } so..... //in instant attached Script function selfDestroy(){ Destroy(gameObject);//SelfDestroy } //and Destroy only the Instant clone Bye !! and luck
(comments are locked)
|
