Issue in a Simple Exercise : a red cube wil spawn: white cube
the white cube will move to the bottom, and when the distance between him and the red cube will be enought, will set a variable in the red cube “spawn.js” script so that the red cube will spawn another one.
Actually the project work very well!
The problem is when i try to destroy a white cube.
when i use:
Destroy(this.gameObject);
All the white will be destroyed…
WHITE CUBE SCRIPT
#pragma strict
private var flag = 1;
public var spawn_distance :float;
//the targhet is my spawn point. my objective is to spawn another of this
//object only when this is far away from the spawn point
public var distance_targhet : GameObject;
function Start () {
}
function Update () {
if (flag == 1 ) {
//ill find the spawn.js actually in the "red" object that is my spawn point
var script : spawn = FindObjectOfType(spawn);
//this object is mooving away from the spawn so when distance is enought ill will make the spawn poit ready to spawn
var distance = Vector3.Distance(gameObject.transform.position, distance_targhet.transform.position);
if (distance > spawn_distance) {
flag=2;
//the dist is OK now "red" will spawn another white cube
script.can_spawn=1;
}
}
if (flag >= 2 ) {
//just to try after a few update i will destroy this object.
flag=flag+1;
Debug.Log(flag);
if (flag == 100 ) {
//THERE ARE MORE THAN 10 CLONE OF THIS OBJECT SPAWNED BUT THIS DESTROY ALL OF THEM .. NOT ONLY THIS ONE ?
Destroy(this.gameObject);
}
}
}
RED CUBE SCRIPT
#pragma strict
//im a red cube on top of the layout
public var can_spawn = 0;
public var continusly_spawn = 0;//if set to 1 i will spawn whithout autorization
public var thing_to_spawn : GameObject; //this thing to spawn is a white cube
//the white cube will move away from my position and will set my "can_spawn" varible when
//the distance between us is enought
function Start () {
}
function Update () {
if( can_spawn == 1 ) { //normally is set to 0, this will be set to 1 by a white cube when distance is enought
Debug.Log("OK READY TO SPAWN"); //i will spawn an object then reset
Instantiate(thing_to_spawn, transform.position, transform.rotation);
can_spawn =0+continusly_spawn;
}
else Debug.Log("NOT READY TO SPAWN");
}