- Home /
code intended to instantiate 7 PathFloorUnit Prefabs,creates only one clone.
I wrote the following code to instantiate the PathFloorUnit Prefab (meant to form path in my running game) and reference it to the Path array.This code instead only forms one instance.So, why is it not working and how do we correct it?
using UnityEngine;
using System.Collections;
public class CreateNew : MonoBehaviour {
//to get reference from original prefab
public Object PathFloorUnit;
//gameObject array created to get reference to instances of instantiated prefab
GameObject[] Path;
void Start () {
//To create instance of PathFloorUnit and assign reference to Path GameObject arrray
for(int x=0;x<=6;x++){
Path[x]=Instantiate(PathFloorUnit,newVector3(0,0,10*x),Quaternion.identity) as GameObject;
}
}
}
Please indent your code correctly. It's very hard to read.
Adding some comments would also help :)
Answer by AjayKhara · Oct 19, 2013 at 05:18 AM
Try this,
using UnityEngine;
using System.Collections;
public class CreateNew : MonoBehaviour
{
//to get reference from original prefab
public Object PathFloorUnit;
//gameObject array created to get reference to instances of instantiated prefab
GameObject[] Path = new GameObject[7];
void Start ()
{
//To create instance of PathFloorUnit and assign reference to Path GameObject arrray
for (int x=0; x<=6; x++) {
Path [x] = Instantiate (PathFloorUnit, new Vector3 (0, 0, 10 * x), Quaternion.identity) as GameObject;
}
}
}
Only problem is that you have not initialized the array. At line no 9. You must initialize your array.
yes it works,thanks yaar.But why doesnt only referencing the array and not creating the gameobjects array actually work(as i did before)?
Because, as any other class, you need to initialize it. The only reason you don't need to initialize something like an int
is that it's a structure, passed by value, not by reference :)
I tried putting the initialization in Start function.It still works,Thanks to your explaination.One more thing, though.I tried initialization without putting an index in GameObject constructor.It didnt work either.So, how do I have control on how many Variable I have in the gameobject array in runtime(you see i want to make a running game and want to target even low end hardware android devices say with less RA$$anonymous$$)
Try looking at different types of collection. What you have there is a static collection or Built-in Array. You can't change it's size. You have to define a size when making a new one as well, or it shouldn't compile.
Yes, if you are using a built in array you must define the size of the array while initializing. If you do not want to do that, use a generic collection. It gives you more control.
See this, it will be really helpful. http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Instantiated prefab name change 3 Answers
Changing LocalScale in code affecting prefab stored in Assets 1 Answer
Local vs. Global movement 5 Answers