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....
var prefab : Transform;
function Start () {
var clone = Instantiate (prefab, Vector3(transform.position.x,
transform.position.y,
transform.position.z),
Quaternion.identity);
}
function Update () {
transform.Rotate(Vector3.up * Time.deltaTime*50);
transform.Rotate(Vector3.right * Time.deltaTime*50);
}
function OnTriggerEnter(collisionInfo : Collider){
Destroy (clone); // <======= THE ERROR IS THIS LINE
Destroy (gameObject);
// Destroy(collisionInfo.gameObject);
}
So my error is the line with :
Destroy (clone);
If anyone could help me, that would be very nice....
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.
Your problem is called variable scoping (what Eric5h5 said, and I voted up his answer for that). To clarify:
var prefab : Transform; // this var is scoped to entire class
function Start () {
// this var is scoped to ONLY the start function
var clone = Instantiate (prefab, Vector3(transform.position.x,
transform.position.y,
transform.position.z),
Quaternion.identity);
}
So all you need to do is this:
var prefab : Transform;
var clone : GameObject; // now available for all functions
function Start () {
clone = Instantiate (prefab, Vector3(transform.position.x,
transform.position.y,
transform.position.z),
Quaternion.identity);
}
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??
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