Question by
EleteKnyte · Jun 11, 2021 at 07:58 PM ·
errorarraysetactiveactive
object.activeSelf and object.activeInheirarchy Both not working
I'm currently writing a weapon switcher script for a learning project in unity, but when I run my code , it never gets past the activeSelf, or in the current code, activeInHeirarchy, if statement.
To give an example, on lines 125 - 135 (in case 1), the code would log "primary active", but never get to "hands deactivated". Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponHandler : MonoBehaviour
{
public GameObject[] infantryRifle;
public GameObject[] scoutRifle;
public GameObject[] sidearm;
public GameObject hands;
public GameObject[] Crosshair;
#region Weapons
// Current equiped assault rifle | 0 = none | infantry rifle var 1 = 1 | infantry rifle var 2 = 2 | etc
public float infantryPrimaryLoadoutIndex;
// Current equiped sidearm | 0 = none | 1 = sidearm var 1 | 1 = sidearm var 2 | etc
public float sidearmLoadoutIndex;
// Current equiped sniper rifle | 0 = none | 1 = scout rifle var 1 | 2 = scout rifle var 2 | etc
public float scoutPrimaryLoadoutIndex;
#endregion
#region Indexes
//Weapon that is current active and usable | 0 = hands | 1 = primary | 2 = sidearm |
public float currentWeaponIndex;
//Class chosen in menu | 0 = infantry | 1 = scout |
public float currentClassIndex;
#endregion
void Start()
{
currentWeaponIndex = 1;
currentClassIndex = 0;
//Deactivate all weapons
foreach (var obj in infantryRifle)
{
obj.SetActive(false);
}
foreach (var obj in scoutRifle)
{
obj.SetActive(false);
}
foreach (var obj in sidearm)
{
obj.SetActive(false);
}
foreach (var obj in Crosshair)
{
obj.SetActive(false);
}
hands.SetActive(false);
}
void Update()
{
//it does what it says
getIndexInput();
//Check what class is selected
switch(((int)currentClassIndex))
{
//Load Correct Class
//Class 0 aka Infantry
case 0:
updateInfantry();
break;
}
}
void getIndexInput()
{
//Get input for weapon index aka select a weapon
if (Input.GetKeyDown(KeyCode.Alpha1))
{
Debug.Log("Key down = 1");
currentWeaponIndex = 1;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
Debug.Log("Key down = 2");
currentWeaponIndex = 2;
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
Debug.Log("Key down = 3");
currentWeaponIndex = 0;
}
}
void updateInfantry()
{
int RLI = ((int)infantryPrimaryLoadoutIndex);
int SLI = ((int) sidearmLoadoutIndex);
GameObject rifle = infantryRifle[RLI];
GameObject side = sidearm[SLI];
switch (((int)currentWeaponIndex))
{
//If hands should be active and usable
case 0:
Debug.Log("hands active");
//if primary or sidearm are active when the primary weapon should be, deactivate them
if (rifle.activeInHierarchy)
{
rifle.SetActive(false);
}
if (side.activeInHierarchy)
{
side.SetActive(false);
}
//if hands are not active, active them
if (!hands.activeInHierarchy)
{
hands.SetActive(true);
}
break;
//If primary infantry weapon should be active and usable
case 1:
Debug.Log("primary active");
//if sidearm or hands are active when the primary weapon should be, deactivate them
if (side.activeInHierarchy)
{
side.SetActive(false);
}
if (hands.activeInHierarchy)
{
hands.SetActive(false);
Debug.Log("hands deactivated");
}
//if currently equiped weapon is not active, activate it
if (!rifle.activeInHierarchy)
{
rifle.SetActive(true);
}
break;
//If sidearm should be active and usable
case 2:
Debug.Log("sidearm active");
//if primary or hands are active when the primary weapon should be, deactivate them
if (rifle.activeInHierarchy)
{
rifle.SetActive(false);
}
if (hands.activeInHierarchy)
{
hands.SetActive(false);
}
//if currently equiped sidearm is not active, activate it
if (side != null)
{
if (!side.activeInHierarchy)
{
side.SetActive(true);
}
}
break;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Unity Inspector public large array - Lag and errors - Optimized GUI Block text buffer too large 0 Answers
Getting an error when using an Array of String Arrays 0 Answers
SetActive the first button that is not active in order of an array 0 Answers
Syncing SetActive over network 0 Answers
Index out of range error 1 Answer