x


cannot create array of gameObjects Instantiate

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 text*this works for 1 but when trying to loop to create multiple occarance it dosnt do any thing I might be missing some thing?

more ▼

asked Jan 01 '12 at 01:48 PM

Tal770 gravatar image

Tal770
16 9 11 12

(comments are locked)
10|3000 characters needed characters left

3 answers: sort newest

I solved it so simple the for (int i=0; i>16; i++){ is wrong its has to be i<16 vuala solve typical programmer haed ake

more ▼

answered Jan 02 '12 at 09:07 AM

Tal770 gravatar image

Tal770
16 9 11 12

(comments are locked)
10|3000 characters needed characters left

I'v done that.. for (int i=0; i>16; i++){ emptyArr[i] = (GameObject)Instantiate(myEmpty, new Vector3(-0.85f, 0.27f+0.773f*i, 8f), Quaternion.Euler(-90,0,0)); emptyArr[i].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[i] = (GameObject)Instantiate(myEmpty, new Vector3(posX[setx], base_y+sety*shelfon, base_z*rowon), Quaternion.Euler(-90,0,0)) as GameObject; emptyArr[i].name= "tray27-" + i; }

}that dosnt work!!!!!!!!!!!!!!!!

more ▼

answered Jan 01 '12 at 02:37 PM

Tal770 gravatar image

Tal770
16 9 11 12

(comments are locked)
10|3000 characters needed characters left

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[i] = (GameObject)Instantiate(myEmpty, new Vector3(-0.85f+i, 0.27f, 8f), Quaternion.Euler(-90,0,0)) as GameObject;
	    }    
	}
more ▼

answered Jan 01 '12 at 02:01 PM

aldonaletto gravatar image

aldonaletto
41.2k 16 42 195

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1355
x13

asked: Jan 01 '12 at 01:48 PM

Seen: 1187 times

Last Updated: Jan 02 '12 at 09:07 AM