I’m adding a script component to an object in a loop like so:
{
clone = (GameObject)Instantiate(hexagon, new Vector3(x,y,z), transform.rotation);
clone.transform.parent = gameObject.transform;
clone.name = "gridTile:" + j + "," + i;
clone.AddComponent<Properties>();
}
Then later on in the code I’m looking for the object that was created (among many) and then setting a value in the Properties (again with a loop so that all objects have values in them) like so (edited to put more code that showed what I did wrong, should’ve made the new string array in the loop itself, thanks @Bonfire-Boy for the comments):
string[] conTemp = new string[6];
for loop{
clone = GameObject.Find("gridTile:" + j + "," + i);
{
string[] conTemp = {some numbers are set during code};
}
clone.GetComponent<Properties>().connections=conTemp;
}
Now the problem is, the code goes through them all and just sets them all the same value instead of different values. If I just run it out of the loop, it sets the right values but the loop somehow breaks it. For example the first one should have something like 1,2,3,4,5,6 as values but the second object should have 2,3,4,5,6,7 and instead they both get 2,3,4,5,6,7.
This is the only thing in the properties script:
public class Properties : MonoBehaviour {
public string[] connections = new string[6];
}
The whole code is uploaded as requested. As it is a messy work in progress code which is really long, I didn’t want to put it in the question itself and couldn’t figure out how to make a spoiler tag if it’s possible, so here it is as an attachment :
[79745-hexagonalhexgenerator.txt|79745]