Gun Switch and SetActive is not Working!

I am making a 2.5D game and I am trying to make gun switching but the problem is that it doesn’t work. There is no gun and there are no errors. What is wrong? (btw the array size is 3)

using UnityEngine;
using System.Collections;

public class InventorySelect : MonoBehaviour {
	
	public  GameObject[] inventoryOfGuns;
	public 	GameObject 	 player;
	public  Transform	 gunPos;
	
	private int			 currentPosInArray;
	
	
	void Awake () 
	{
		currentPosInArray = 0;
		
		for(int i = 0; i < 3; i++)
		{
			GameObject gunInstance = (GameObject)Instantiate(inventoryOfGuns*, gunPos.position, Quaternion.identity);*
  •  	gunInstance.transform.parent = player.transform;*
    

_ inventoryOfGuns*.SetActive(false);_
_
}_
_
inventoryOfGuns[0].SetActive(true);*_

* }*

* void Update ()*
* {*
* if(Input.GetKeyDown(KeyCode.E))*
* {*
* inventoryOfGuns[currentPosInArray].SetActive(false);*
* if(currentPosInArray == 2)*
* {*
* currentPosInArray = 0;*
* }*
* else*
* {*
* currentPosInArray++;*
* }*
* Debug.Log(currentPosInArray);*
* inventoryOfGuns[currentPosInArray].SetActive(true);*
* }*

* if(Input.GetKeyDown(KeyCode.Q))*
* {*
* inventoryOfGuns[currentPosInArray].SetActive(false);*
* if(currentPosInArray == 0)*
* {*
* currentPosInArray = 2;*
* }*
* else*
* {*
* currentPosInArray–;*
* }*
* Debug.Log(currentPosInArray);*
* inventoryOfGuns[currentPosInArray].SetActive(true);*
* }*

* }*
}

In this attachment I’ve sent 2 classes that should be sufficient for you to get the grasp of how this can be done, the code is untested (just wrapped it up in notepad right now)
I’ve used this kind of technique before however.

[12897-classes.zip|12897]

Two possibilities, the objects populating your inventory are either prefabs(A) or actual scene objects(B).

using UnityEngine;
using System.Collections;
 
public class InventorySelect : MonoBehaviour {
 
    public  GameObject[] inventoryOfGuns;
    GameObject [] weapons = new GameObject[3];
    public     GameObject      player;
    public  Transform   gunPos;
    private int          currentPosInArray;

    void Awake () {
       currentPosInArray = 0;
       for(int i = 0; i < 3; i++)
       {
         weapons _= (GameObject)Instantiate(inventoryOfGuns*, gunPos.position, Quaternion.identity);*_

weapons*.transform.parent = player.transform;*
weapons*.SetActive(false);*
}
weapons[0].SetActive(true);
}

void Update ()
{
if(Input.GetKeyDown(KeyCode.E))
{
weapons[currentPosInArray].SetActive(false);
if(currentPosInArray == 2)
{
currentPosInArray = 0;
}
else
{
currentPosInArray++;
}
Debug.Log(currentPosInArray);
weapons[currentPosInArray].SetActive(true);
}

if(Input.GetKeyDown(KeyCode.Q))
{
weapons[currentPosInArray].SetActive(false);
if(currentPosInArray == 0)
{
currentPosInArray = 2;
}
else
{
currentPosInArray–;
}
Debug.Log(currentPosInArray);
weapons[currentPosInArray].SetActive(true);
}
}
}
B version:

void Awake ()
{
currentPosInArray = 0;

for(int i = 0; i < 3; i++)
{
inventoryOfGuns*.transform.position = gunPos.position;*
inventoryOfGuns*.transform = player.transform;*
inventoryOfGuns*.SetActive(false);*
}
inventoryOfGuns[0].SetActive(true);
}
The rest remains the same as your example