In my game, I instantiate weapons when I equip them. I have a list of transforms as my list of weapons, and my selected weapon is also a transform. when I press the key I need to press in order to equip a weapon, it instantiates the weapon into my players hand. The problem I have is with destroying the equipped weapon when I want a different weapon. Every time I press the “switchWeapon” key, it cycles through my list and gives me the next weapon in the list.
How the h**l do I upload code on here?
My problem is specifically with those last couple lines ^^
I’m getting an “Object reference not set to an instance of an object”
Each weapon should be a prefab in your project.
When you instantiate it, the code should look something like this…
[SerializeField] GameObject _myWeaponPrefab;
GameObject weapon = GameObject.Instantiate(_myWeaponPrefab, player.transform.position, Quaternion.identity);
You now have a reference to the instantiated weapon, which you can destroy by simply calling
Greg, you solved my issue, but I can’t vote for your answer. My issue was that I had a prefab declared as a Transform. I cast it from the Instantiate method as a GameObject with no errors or warnings. Called Destroy with no errors or warnings. However, the prefab would remain on the screen. Declaring my prefab as a GameObject fixed that issue. Many thanks.