- Home /
Code only affects first clone?
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);
}
}
Answer by Jammer3000 · May 22, 2014 at 12:58 PM
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[i].transform.position.x < cloudsResetPositionPosition)
{
evilCloudsArray[i].transform.position = gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0);
}
}
}
Answer by ASPePeX · May 21, 2014 at 01:37 AM
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 ASPePeX what do you mean use an array? For like the the evil cloud clone?
Yes. You instantiate 10 clones, but only have one reference. The other 9 are just floating there. You need to store all 10 references in an array and then iterate through that array in RespawnEvilCloud(), calling the code for each clone.
Is there anyway I could get a example of how this would be done? This is how I tried doing it but it doesn't affect anything? I also assigned the object being instantiated a tag of EvilCloud which should give all its clones that tag too.
#pragma strict
private var evilCloudClone : Transform;
var evilCloud : Transform;
var cloudDiesPosition = -3;
public var clouds : GameObject[];
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);
}
clouds = GameObject.FindGameObjectsWithTag("EvilCloud");
Debug.Log("Found Objects with tag EvilCloud");
}
function Update () {
RespawnEvilCloud();
}
function RespawnEvilCloud () {
// Deter$$anonymous$$s when to delete evil cloud and respawn
for (var i : int = 0; i < clouds.Length; i++) {
Debug.Log("Ran loop");
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);
}
}
}
In the second loop, you are using the same old reference. evilCloudClone still holds just one cloud. I don't use javascript so its hard for me to give a proper code example, but I think you need to use clouds[i] in the second loop ins$$anonymous$$d of evilCloudClone.