- Home /
Question by
CrosspadStudios · Jul 07, 2013 at 03:32 PM ·
instantiatearrayobjectnaming
How do I name cloned objects inside a for-loop?
So I'm new C# (or any programming, for that matter) and I'm stuck. I've written a script that generates a 4x4 tile game board that uses a tile prefab I made. All that's working fine, and I even started playing around with the array I'll need, but I can't seem to figure out how to give each of the clones unique names, like tile00, tile01, tile02, etc. Any help would be greatly appreciated! I'll attach my code as a reference, feel free to point out any mistakes.
Thanks!
using UnityEngine; using System.Collections;
public class instantiateBoard : MonoBehaviour {
public GameObject tile = null;
public float rowPosition = 1.5f;
public float colPosition = -1.5f;
public int count = 1;
public float[,] gameBoard = new float[4,4]; //New to arrays so I'm practicing generating values inside a for-loop
public int rowCount = 0;
public int colCount = 0;
void Start ()
{
for (int count2 = 1; count2 < 5; count2++)
{
for (int count = 1; count <5; count++)
{
tile = Instantiate(Resources.Load("Prefabs/Cube"), new Vector3(colPosition,0,rowPosition), Quaternion.identity) as GameObject;
name = ("tile" + rowCount + colCount);
gameBoard[rowCount,colCount] = (rowPosition + colPosition);
colPosition = (colPosition + 1f);
colCount = (colCount + 1);
}
colPosition = -1.5f;
rowPosition = (rowPosition - 1f);
colCount = 0;
rowCount++;
}
}
}
Comment
Please format code properly by selecting it all and hitting the code button.