For some reason I can’t get a reference to an object that I’m instantiating:
public Transform LocationPrefab;
public static GameObject[,] ArrayOfLocations = new GameObject[48, 15];
public LocationScript LocScr;
public static float Radius = 4;
public void BuildPlanet()
{
for (int counterx = 0; counterx < ArrayOfLocations.GetLength(0); counterx++)
{
for (int countery = 0; countery < ArrayOfLocations.GetLength(1); countery++)
{
Vector3 locationPosition = new Vector3(counterx, countery, 0);
LocScr = Instantiate(LocationPrefab, Vector3.zero, Quaternion.identity) as LocationScript;
print(LocScr);
LocScr.inclination = 90 - (15 * (countery + 5) / 2);
LocScr.azimuth = 7.5f * counterx;
LocScr.transform.Translate(Radius, 0, 0);
LocScr.transform.RotateAround(Vector3.zero, Vector3.forward, LocScr.inclination);
LocScr.transform.RotateAround(Vector3.zero, Vector3.up, LocScr.azimuth);
ArrayOfLocations[counterx, countery] = LocScr.gameObject;
}
}
}
Result:
Prints “null” and than “NullReferenceException: Object reference not set to an instance of an object”.
I have no idea why won’t it reference it, even the example in scripting reference looks similar. var = instantiate () as class; and one can than use this var when it’s needed later on. What am I missing?