- Home /
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[i], gunPos.position, Quaternion.identity);
gunInstance.transform.parent = player.transform;
inventoryOfGuns[i].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);
}
}
}
I'm confused, you have the inventory array that seem to be prefab since you create object of them in the awake.
Then you attach the instance to the player and deactivate the prefab. Later on you manipulate the prefabs in the array but never the instances you created. Is that fully intended?
void Awake () {
currentPosInArray = 0;
for(int i = 0; i < 3; i++){
GameObject gunInstance = (GameObject)Instantiate(inventoryOfGuns[i], gunPos.position, Quaternion.identity);
gunInstance.transform.parent = player.transform;
inventoryOfGuns[i].SetActive(false);
}
inventoryOfGuns[0].SetActive(true);
}
this is your awake, you create three objects of the type of the inventory in gunInstance but you do not do anything with those. They are created and not use nor destroyed. Well, you attach them bu that is it. On the other hand, you have your inventory that you are activating and not. I would guess this inventory has the things you need but as they are not attached to the player, they just sit somewhere in the scene.
Answer by fafase · Jul 08, 2013 at 02:23 AM
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[i] = (GameObject)Instantiate(inventoryOfGuns[i], gunPos.position, Quaternion.identity);
weapons[i].transform.parent = player.transform;
weapons[i].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[i].transform.position = gunPos.position;
inventoryOfGuns[i].transform = player.transform;
inventoryOfGuns[i].SetActive(false);
}
inventoryOfGuns[0].SetActive(true);
}
The rest remains the same as your example
Interesting let me test this code out. $$anonymous$$now I know what you mean by prefab instance. Thanks
Answer by xortrox · Jul 08, 2013 at 01:57 AM
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.
forgot to add that the currently spawned weapon will be accessible from the Player class as weapon.weapon$$anonymous$$odel.