Question by
darkmoosen · Apr 22, 2018 at 04:31 PM ·
gameobjectassign-variableunassignedreferenceexcept
UnassignedReferenceException when the Variable is assigned to my GameObject
Hey guys, I'm having an issue where I have a Character Spawner that is trying to create Characters, and place them within a certain Canvas. To do that, I have a script that creates the character, and then sets the Canvas as the parent. However, every time I run my code now it says that said Variable hasn't been assigned, when it has, as shown in this screenshot. This was working for me yesterday, but I changed the button from a generic rectangle to a different sprite. However, my code isn't crashing in the script where I changed the button, it's crashing where my spawner is. The code I'm using right now is located below.
public GameObject ForeGroundCanvas;
public static GameObject created;
public GameObject[] cat;
public Transform[] spawnPoints;
public static int catSelectionIndex;
public static int spawnPointIndex;
public void Spawn()
{
//creates a random index point for our spawn points.
spawnPointIndex = Random.Range(0, spawnPoints.Length);
catSelectionIndex = Random.Range(0, cat.Length);
//creates an instance of the a cat, and places it within our Canvas
created = Instantiate(cat[catSelectionIndex], spawnPoints[spawnPointIndex]);
created.transform.SetParent(ForeGroundCanvas.transform);
created.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
return;
}
Edit: The line of code having issues is line 17 "created.transform.SetParent(ForegroundCanvas.transform);
uhg.png
(13.5 kB)
Comment