- Home /
How do I use array's to spawn gameobjects(C#)?
I am wondering how I am can spawn my prefabs, 3 different squares, to make up a chessboard. I have been looking at some tutorials, but I can't find anything that particularly have helped me to do this, any tips or useful turorials into understanding arrays is helpful.
Answer by aldonaletto · Jan 31, 2013 at 11:51 PM
Not sure if I correctly understood your problem, but suppose that you could declare a public array and use its elements, like this:
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject prefabs[]; // populate the array in the Inspector
// set the index to select some specific prefab:
...
Vector3 pos = ... // calculate position
Quaternion rot = ... // define rotation
int index = ... // select prefab index
Instantiate(prefabs[index], pos, rot);
...
}
}
The array prefabs appears as Prefabs in the Inspector: you must set its length and drag the prefabs to the slots.
I've looked at what you showed me here, but I encounter an error on this line: "public GameObject prefabs[];" It won't work for me. I don't have much knowledge when it comes to scripting at all, but I tried writing it like this which didn't give me an error but I don't think it works like you meant it to.
public GameObject[] prefabs = new GameObject[1];
that gives me 1 item, I just have to declare which item in the script if I think correctly, something like:
prefabs[0] = "brett";
That's probably wrong, not sure.
To the pos, rot and that. The pos = new Vector3 (f,f,f)? and rot = Quaternion (f,f,f)? About the index, how do I select that and what should I put in after int index = ?
I don't know much about scripting and the tutorials I've found so far on array's are only about listing names not objects and placing, if you know of any tutorial that touches these subjects please let me know.
Answer by StianH · Feb 01, 2013 at 12:21 AM
Thanks for the answer, to take it abit further. Is there a way to instantiate a chessboard with a different prefab in the center, and edges of the board? say something like this:
2,1,1,1,2
1,1,3,1,1
2,1,1,1,2
the numbers represent a different color and potential property. 1 being standard, 2 and 3 having some different properties/colors.
So what he showed you is a manually set array where you drop things in with the inspector. You can alternatively populate the array by using .push(gameobject).
So either create your attributes in advance and populate them in the inspector, or use a script to create new prefabs with the properties and then push them to the array.
Answer by Jonde · Jan 28, 2014 at 04:39 PM
Create the array like you wrote there {2,1,1,1,2,1,1,3,1,1,2,1,1,1,2}. Then go it through with for (int i = 0; i < array.Lenght; i++){ instantiate things here }
.
Then you just instantiate the game objects you want at each spot. Everytime you instantiate an object, you add 1f (or whatever the width of the object is) unit more to the next objects position.x. Position.Y get's also added but naturally after you have reached the end of each row.
I hope this helps you to figure it out!