- Home /
Unassigned Reference Exception on assigned variable?
Within a script, on the Start() function, I assign a GameObject variable bulletSpawnSpot to a child of the object. This child is where i want bullets to spawn from, it does nothing more. I do not have the bulletSpawnSpot variable assigned prior to launching the game, it is only assigned after it runs it's Start() function. upon running a debug after assigning bulletSpawnSpot, it prints that indeed, bulletSpawnSpot is assigned to GameObject BulletSpawnPos (name of the child). But when i call the FireShot() function, I use bulletSpawnSpot.transform.position and bulletSpawnSpot.transform.rotation to spawn the bullet at the game object, but i get the following error when i try to fire http://imgur.com/iKdUJMu Here's the code for assignment
void Start ()
{
player = GameObject.FindWithTag("Player");
cam = Camera.main;
foreach(Transform child in transform)
{
if(child.gameObject.tag == "PlayerBulletSpawn")
bulletSpawnSpot = child.gameObject;
}
Debug.Log(bulletSpawnSpot.ToString());
}
Again, that debug prints that bulletSpawnSpot = BulletSpawnPos, which is what i want. but when i call the FireShot function...
public void FireShot(int bulletType)
{
if(bulletType == 1)
PhotonNetwork.Instantiate("Orange_Bullet", bulletSpawnSpot.transform.position, bulletSpawnSpot.transform.rotation, 0);
Debug.Log("FireShot");
}
...it gives me the error posted above. Also, I'm calling the FireShot() function from the player's handler script
if(Input.GetButtonDown("Fire1"))
{
bulletScript.FireShot(currentWeapon);
}
Not sure if that would affect it. Any ideas?
P.S. I'm rather new to Unity (started just over a week ago) so if this is some noob mistake, that's why c:
Answer by ThomasMarsh · Jan 10, 2015 at 03:16 AM
You could try referencing it in a different way than you currently are. If only one object is tagged with the "PlayerBulletSpawn" tag then you could just reference it by saying bulletSpawnSpot = GameObject.FindGameObjectWithTag("PlayerBulletSpawn");
Additionally, the fact that you are calling it from another script could mean that it somehow has not been assigned in the original script.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180017im_/https://answers.unity.com/themes/thub/images/avi.jpg)