Hi Jeremy here! The code below works great but only for the first clone that is instantiated? I need it to work for all the clones and I have never had this problem before and can’t seem to quite understand why? Any help is greatly appreciate thank you (:
#pragma strict
private var evilCloudClone : Transform;
var evilCloud : Transform;
var cloudDiesPosition = -3;
function Start () {
// Instantiate the evilCloud and then add Random positions to its x and y positions
for (var i : int = 0; i < 10; i++) {
evilCloudClone = Instantiate(evilCloud, gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0), Quaternion.identity);
}
}
function Update () {
RespawnEvilCloud();
}
function RespawnEvilCloud () {
// Determins when to delete evil cloud and respawn
if (evilCloudClone.transform.position.x < cloudDiesPosition) {
Debug.Log("Cloud Deleted");
evilCloudClone.transform.position = gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0);
}
}
It looks like it should only be working for the last clone. You are overwriting evilCloudClone in every loop, so when the for loop is done evilCloudClone only holds the last clone. Use an array and you are good.
Thanks Good old Grim that was it here is the working code! Thanks to everyone that helped!
#pragma strict
private var evilCloudClone : Transform;
private var evilCloudsArray : GameObject[];
var evilCloud : Transform;
var cloudsResetPositionPosition = -4;
function Start ()
{
// Instantiate the evilCloud and then add Random positions to its x and y positions to simulate random spawning
for (var i : int = 0; i < 10; i++)
{
evilCloudClone = Instantiate(evilCloud, gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0), Quaternion.identity);
}
// Fill our clouds array with all gameobjects with tag EvilCloud(fills array with all evil cloud gameobjects)
evilCloudsArray = GameObject.FindGameObjectsWithTag("EvilCloud");
}
function Update ()
{
RespawnEvilCloud();
}
function RespawnEvilCloud ()
{
// Resets position of clouds(array) objects to random position on right side of screen and resets the positions of x and y for randomness
for (var i : int = 0; i < evilCloudsArray.Length; i++)
{
if (evilCloudsArray*.transform.position.x < cloudsResetPositionPosition)*
{
evilCloudsArray*.transform.position = gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0);*
}
}
}