I am using the following code in a game I am developing (irrelevant code removed):
public class WeaponSpawner : MonoBehaviour
{
GameObject weapon;
public void UseWeapon()
{
Instantiate(weapon, transform.position, Quaternion.Euler(direction), transform);
}
public void UpdateWeapon(GameObject newWeapon)
{
weapon = newWeapon;
}
}
I call the UpdateWeapon() method from the game menu when changing the active weapon, and I call the UseWeapon() method when actually attacking.
For some reason, however, even though the UpdateWeapon() works fine (confirmed with Debug.Log() and inspector while game is running) assigning the new weapon, when I attempt to call UseWeapon() afterwards, I get the following error:
"UnassignedReferenceException: The variable weapon of WeaponSpawner has not been assigned."
Why is this happening/what is going on?
For some reason, however, even though the UpdateWeapon() works fine (confirmed with Debug.Log() and inspector while game is running) assigning the new weapon, when I attempt to call UseWeapon() afterwards, I get the following error:
"UnassignedReferenceException: The variable weapon of WeaponSpawner has not been assigned."
Why is this happening/what is going on?