My player is without a weapon by default. I want him to be able to pick up guns and switch guns.
My first thought was to just make every single gun you can get, a child of his right forearm, and disable them. This way they are hidden in the background and their scripts wont run in the meantime. The game will patiently wait until you hit a pickup, then enable it.
So I place this little code on my pickup, as a test:
private GameObject _rifle;
public void OnCollisionEnter(Collision collision)
{
_rifle = GameObject.Find("Rifle");
_rifle.SetActive(true);
}
Sadly, this doesn’t work. Unity tells me: “Object reference not set to an instance of an object”
It turns out this may be, because Unity can’t find objects that are disabled. So now I’m a little lost about how to work around this issue. Can you guys help me out?
UPDATE:
I got it to work temporarily now with this script:
public GameObject WeaponGameObject; //The object you want to activate
private void OnCollisionEnter(Collision collision)
{
WeaponGameObject.SetActive(true);
}
}
But sadly I feel this is a temporary solution, because if the player is not in the scene when I was designing the level I can’t actually place his weapon in the public game object slot.