Why aren’t these “if” statements working?

Does anybody have any idea why these “if” statements aren’t working? The objects “weaponSlot_1” and “weaponSlot_2” are assigned properly in the inspector, but the “if” statements still don’t work.

    if (weaponSlot_1.activeInHierarchy == true)
       {
           SwitchActiveSlot(0);
           Debug.Log("Loadout Slot Switched to 0");
       }
      
    if (weaponSlot_2.activeInHierarchy == true)
       {
           SwitchActiveSlot(1);
           Debug.Log("Loadout Slot Switched to 1");
       }

Any help at all would be deeply appreciated as I’m just stuck.

If any more information is needed I’m happy to share it.

Did you try adding debug.log calls to see what activeInHierarchy is showing? Did you add Debug.log calls to even make sure the method those if statements are in is being run?

Unfortunately showing if statements without context doesn’t help. Because at that point all we can say is the if statement isn’t working because the condition to satisfy that if statement isn’t true.

How would I go about doing that? I’m not very advanced with coding. Also, I can post the full script if that would help. Thank you for answering

I see you have some Debug.Log calls within your if statements. Put one outside of those if statements to check if the method is even being called.

The add a couple more debug.log statements that print out the values of weaponSlot_2.activeInHierarchy and weaponSlot_1.activeInHierarchy

Start there, and yes, it’s a good idea to post the script when asking for coding help. +1 though for using code tags!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponManager : MonoBehaviour
{
  public static WeaponManager Instance { get; set; }
 
  public List<GameObject> weaponSlots;

  public GameObject activeWeaponSlot;
public Weapon weapon;
public GameObject weaponSlot_1;
public GameObject weaponSlot_2;
  public InputManager inputManager;
   private void Awake()
    {
      if (Instance != null && Instance != this)
   {
       Destroy(gameObject);
   }
   else
   {
       Instance = this;
   }
   }
  
   private void Start()
   {
      activeWeaponSlot = weaponSlots[0];
   }
  
   private void Update()
   {
   foreach (Transform weapon in transform)
   {
      foreach (GameObject weaponSlot in weaponSlots)
   {
   if (weapon.gameObject.activeInHierarchy == true && weaponSlot == activeWeaponSlot)
   {
       weaponSlot.SetActive(true);
   }  
   else
   {
       weaponSlot.SetActive(false);
   }
  
    if (weaponSlot_1.activeInHierarchy == true)
       {
           SwitchActiveSlot(0);
           Debug.Log("Loadout Slot Switched to 0");
       }
      
    if (weaponSlot_2.activeInHierarchy == true)
       {
           SwitchActiveSlot(1);
           Debug.Log("Loadout Slot Switched to 1");
       }
   }
   }
  
  
  
  
   }
 
   public void PickupWeapon(GameObject pickedupWeapon)
   {
       AddWeaponIntoActiveSlot(pickedupWeapon);
   }
  private void AddWeaponIntoActiveSlot(GameObject pickedupWeapon)
  {
 
  DropCurrentWeapon(pickedupWeapon);
 
 
     pickedupWeapon.transform.SetParent(activeWeaponSlot.transform, false);
  
   Weapon weapon = pickedupWeapon.GetComponent<Weapon>();
  
   pickedupWeapon.transform.localPosition = new Vector3(weapon.spawnPosition.x, weapon.spawnPosition.y, weapon.spawnPosition.z);
   pickedupWeapon.transform.localRotation = Quaternion.Euler(weapon.spawnRotation.x, weapon.spawnRotation.y, weapon.spawnRotation.z);
  
   weapon.isActiveWeapon = true;
  }
 
  private void DropCurrentWeapon(GameObject pickedupWeapon)
  {
     if (activeWeaponSlot.transform.childCount > 0)
   {
       var weaponToDrop = activeWeaponSlot.transform.GetChild(0).gameObject;
      
       weaponToDrop.GetComponent<Weapon>().isActiveWeapon = false;
      
       weaponToDrop.transform.SetParent(pickedupWeapon.transform.parent);
       weaponToDrop.transform.localPosition = pickedupWeapon.transform.localPosition;
       weaponToDrop.transform.localRotation = pickedupWeapon.transform.localRotation;
   }
  }
 
  public void SwitchActiveSlot(int slotNumber)
  {
     if (activeWeaponSlot.transform.childCount > 0)
   {
       Weapon currentWeapon = activeWeaponSlot.transform.GetChild(0).GetComponent<Weapon>();
       currentWeapon.isActiveWeapon = false;
   }
  
   activeWeaponSlot = weaponSlots[slotNumber];
  
        if (activeWeaponSlot.transform.childCount > 0)
   {
       Weapon newWeapon = activeWeaponSlot.transform.GetChild(0).GetComponent<Weapon>();
       newWeapon.isActiveWeapon = true;
   }
  
  }
 
 
}

Here’s the code. I can’t figure out how to put Debug Logs outside the “if” statements and get them to show

Debug.Log(weaponSlot_1.activeInHierarchy);

if(weaponSlot_1.activeInHierarchy == true)
{
    //Do stuff if true.
}

Pretty simple. Now, if that Debug call doesn’t show in Unity’s Console, then your Update isn’t running. Which usually means the gameobject the script is on is off. So, make sure the gameobject this script is attached to isn’t off.

I’m not sure what the big mystery is here.

Debug.Log inside the if statement

if (weaponSlot_1.activeInHierarchy == true)
       {
           SwitchActiveSlot(0);
           Debug.Log("Loadout Slot Switched to 0");
       }

Debug.Log outside the if statement:

Debug.Log("Loadout Slot Switched to 0");
if (weaponSlot_1.activeInHierarchy == true)
       {
           SwitchActiveSlot(0);
       }

Maybe you meant that you’ve already tried this and you still don’t see anything? In that case your foreach loops are probably not looping.

Also make sure you don’t have debug messages turned of in your console.

There’s also a debugger in your IDE that you can attach, set breakpoints, then step over the code line by line as it executes, at the same time you can view the values of all variables. Consider how quickly that’ll explain the code’s behaviour, and then you’ll want to figure out how that works (don’t ask: there’s tons of tutorials available :wink: ).