for a single object I instantiate it ok
but when trying to instantiate multiple objects of it nothing happend.
the code:
using UnityEngine;
using System.Collections;
public class setEmptyTrays : MonoBehaviour {
public GameObject myEmpty;
public GameObject[] emptyArr = new GameObject[16];
// spread empty trays on shelp
void Awake() {
// remarking the loop:
for (int i=0; i>16; i++){
emptyArr[1] = (GameObject)Instantiate(myEmpty, new Vector3(-0.85f, 0.27f, 8f), Quaternion.Euler(-90,0,0)) as GameObject;
}
}
}
strong textthis works for 1 but when trying to loop to create multiple occarance it dosnt do any thing
I might be missing some thing?
You’re creating all objects in the same place, and storing all of them in the same variable (emptyArr[1]). You should use i as the emptyArr index, and do something to have different positions for each new object - add i to the x coordinate, for instance:
void Awake() {
// remarking the loop:
for (int i=0; i>16; i++){
// change the index to i and use x+i as a pos offset, for instance:
emptyArr *= (GameObject)Instantiate(myEmpty, new Vector3(-0.85f+i, 0.27f, 8f), Quaternion.Euler(-90,0,0)) as GameObject;*
* }*
* }*
*
I’v done that…
for (int i=0; i>16; i++){
emptyArr = (GameObject)Instantiate(myEmpty, new Vector3(-0.85f, 0.27f+0.773f*i, 8f), Quaternion.Euler(-90,0,0)); emptyArr*.name= “tray27-” + i;* * }* and it dosnt work? all elements in the array are set as none and not receiving a name. I just wanted to show that runnung for a single instant it works here is the actual code i use: public class setEmptyTrays : MonoBehaviour { * public GameObject myEmpty;* * public GameObject[] emptyArr = new GameObject[16];* * public float base_y = 0.27f;
public float base_z = 8.57f;
_ public float rowon = 4.0f;_
_ public float shelfon = 0.7729f;_
_ public float posX = new float { -0.85f, 4.2f, 20.3f, 25.34f };_
_ // spread empty trays on shelp*_ * void Awake() {* * for (int i=0; i>16; i++){* * int setx = Random.Range(1,4);* * int sety = Random.Range(1,70);* * int setz = Random.Range(1,10);* emptyArr = (GameObject)Instantiate(myEmpty, new Vector3(posX[setx], base_y+setyshelfon, base_zrowon), Quaternion.Euler(-90,0,0)) as GameObject; _ emptyArr*.name= “tray27-” + i; }*_