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.
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.
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.
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!