- Home /
Clone Prefab Duplicate Problem
Alright well, I'm making an asteroid field with Instantiate(asteroid, position, Quaternion.identity);
where asteroid is a gameobject - prefab,
and now by doing that i make about 100 clones give or take, and on the prefab i have a script that has a raycast, and a random given number from 20 to a 100,
now I want each asteroid's script to be valued as individual instead if i select one asteroid all clones get selected, and if i want to destroy an asteroid after some period of time all clones of the same kind gets destroyed,,
anyone know how to help me so the script/clones are valuead as individual gameobjects or whatever,,, show examples if you can please and thanks.
Here's the code:
var asteroid : GameObject; var asteroid2 : GameObject; var asteroid3 : GameObject; var asteroid4 : GameObject;
var times = 0; var go : boolean = true; function Update () {
if(go) {
times++;
var position = Vector3(Random.Range(-2000, 2000), 0, Random.Range(-2000, 2000));
var position2 = Vector3(Random.Range(-2000, 2000), 0, Random.Range(-2000, 4000));
var position3 = Vector3(Random.Range(-2000, 2000), 0, Random.Range(-2000, 4000));
var position4 = Vector3(Random.Range(-2000, 2000), 0, Random.Range(-2000, 4000));
Instantiate(asteroid, position, Quaternion.identity);
Instantiate(asteroid2, position2, Quaternion.identity);
Instantiate(asteroid3, position3, Quaternion.identity);
Instantiate(asteroid4, position4, Quaternion.identity);
if(times >= 55) { go = false;
}
}
}
THE ASTEROIDS SCRIPT:
var minerals = 0; var mayI : boolean = true;
function Update () { if(mayI) { minerals = Random.Range(30, 100); mayI = false; Debug.Log(minerals); }
if (Input.GetButtonDown ("Fire1")) { var hit : RaycastHit; if(Physics.Raycast(transform.position, transform.forward, hit)) { hit.transform.SendMessage("HitByRaycast",gameObject, SendMessageOptions.DontRequireReceiver); }
}
} // int = parseInt(variablename);
// int myBlubb = myFloatBlubb as int; //int myBlubb = (int) myFloatBlubb;
function HitByRaycast(source : GameObject) { Debug.Log("i've been hit"); Debug.Log(minerals); // Debug.Log(minerals); }
Answer by s4vi0r · Dec 29, 2010 at 06:12 PM
When you instantiate a prefab the script attached to each one should be unique to that object. As far as you problem accessing the prefabs... It sounds like a naming problem. When you instantiate something just change its name to something unique..You could even build an array if necessary. Try something like this:
var asteroids : array;
...... asteroid = new array(); for(i = 0; i < 20) {
var newWorldObject = Instantiate(asteroid, Vector(0,0,0), Quaternion.identity);
//assigns names "asteroid0 - asteroid19" and adds them to array.
newWorldObject.name = ("asteroid" + i);
asteroids.Add(newWorldObject);
}
Hope this helps.