Switching the index of a GameObject Array type?
Hi, I made more than a weapon in Unity, but now I need something to switch the
public GameObject[] inventory;
But later, the compiler saied I can't use integer to switch the index of a gameObject Array.
I need to set active by integer the gameObject stored in a slot (1,2,3 etc).
Moreover the weapon should be changable by holding E, and they are:
1 = Melee, 2 = Pistols, 3 = Shotguns etc Like a fps arena... but the question is just for the gameObject array use.
Some help? Thanks! :)
can you send us the part of code you've writted about this function ?
I responded down(It's strange that the tag @Jacky-$$anonymous$$arley didn't find you, so I erased it).
Answer by Jacky-Marley · Jan 27, 2017 at 10:30 AM
void ChangeWeapon()
{
foreach ( GameObject weapon in inInventory)
{
weapon.setActive(false);
}
inInventory[indexOfTheWeapon].setActive(true);
}
It seems to be ok as code, but now there are problems with the integration.
void ChangeWeapon()
{
foreach (GameObject activeWeapon in inInventory)
{
activeWeapon.SetActive(false);
}
inInventory[(int)INVENTORY_$$anonymous$$axSlot.Slot1].SetActive(true);
}
What happens is each slot selects the same type of weapon. Anyway I'll try to debug, thanks! :)
Answer by Ragatto · Jan 26, 2017 at 02:53 PM
During this time I modified the code: I used enums, so now it works! The only problem I don't know how active the wanted slot, deactivating the other ones.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponSelection : MonoBehaviour {
public GameObject[] inInventory = new GameObject[(int)INVENTORY_Melee.SlotCount];
private GameObject activeWeapon;
public enum INVENTORY_Melee
{
Empty,
Hammer,
SlotCount
}
public enum INVENTORY_Guns
{
Empty,
NailGun,
RevolverM_500,
SlotCount
}
public enum INVENTORY_ShotGuns
{
Empty,
SlotCount
}
public enum INVENTORY_MachineGuns
{
Empty,
SlotCount
}
public enum INVENTORY_Explosives
{
Empty,
SlotCount
}
public enum INVENTORY_Rifles
{
Empty,
SlotCount
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.anyKeyDown) {
switch (Input.inputString)
{
case "0":
break;
case "1":
GameObject activeMelee = inInventory[(int)INVENTORY_Melee.Hammer - 1];
activeWeapon = activeMelee;
break;
case "2":
GameObject activeGun = inInventory[(int)INVENTORY_Guns.NailGun];
activeWeapon = activeGun;
break;
case "3":
GameObject activeShotguns = inInventory[(int)INVENTORY_ShotGuns.Empty];
break;
case "4":
GameObject activeMachineGuns = inInventory[(int)INVENTORY_MachineGuns.Empty];
break;
case "5":
GameObject activeExplosives = inInventory[(int)INVENTORY_Explosives.Empty];
break;
case "6":
GameObject activeRifles = inInventory[(int)INVENTORY_Rifles.Empty];
break;
default:
break;
}
}
}
}
I tried using gameObject.setActive(bool), but (again, )I need something which I can set active the gameobject in the slot I selected, setting not-active the other ones.
Just this for now.