I have a double if statement. I am trying to reference a variable named obj that is declared in the first if statement. In the second statement I am trying to reference this variable so that I can unparent it. I have all of the code correct for making the parent null but for the life of me I can’t find out how to reference the obj variable. In my current build it says “NullReferenceException” when i attempt to use the obj variable in my second if statement.
Below is my code.
var respawnPrefab : GameObject;
var respawnPrefab1 : GameObject;
var respawnPrefab2 : GameObject;
var respawn : GameObject;
function Start()
{
if (respawn==null)
respawn = GameObject.FindWithTag ("Respawn");
}
function Update()
{
var objs = new GameObject[3];
objs[0] = respawnPrefab;
objs[1] = respawnPrefab1;
objs[2] = respawnPrefab2;
if (Input.GetKeyDown (KeyCode.Mouse0))
{
var obj = Instantiate(objs[(Random.Range(0, objs.Length))], respawn.transform.position, respawn.transform.rotation);
obj.transform.parent = Camera.main.transform;
}
if (Input.GetKeyUp (KeyCode.Return))
{
obj.transform.parent = null;
}
}