How to switch between two tags being active in a script

(this is my first game so sorry if this is a stupid question) So I want every gameobject with the tag “reality1” to turn off and every gameobject tagged “reality2” to turn on when I press shift, then the opposite to happen when I press shift again. right now I have

using UnityEngine;

public class LayerToggle : MonoBehaviour
{

   bool Reality2 = false;

   private void Update()
   {
       if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
       {
           shift();
       }
   }

   void shift()
   {
       if (!Reality2)
       {
           GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("reality1");

           foreach (GameObject go in gameObjectArray)
           {
               go.SetActive(false);
               Debug.Log("reality1 off");
           }

           GameObject[] gameObjectArray2 = GameObject.FindGameObjectsWithTag("reality2");

           foreach (GameObject go2 in gameObjectArray2)
           {
               go2.SetActive(true);
               Debug.Log("reality2 on");
           }
           Reality2 = true;
           Debug.Log("reality2");
       }
       else
       {
           GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("reality1");

           foreach (GameObject go in gameObjectArray)
           {
               go.SetActive(true);
               Debug.Log("reality1 on");
           }

           GameObject[] gameObjectArray2 = GameObject.FindGameObjectsWithTag("reality2");

           foreach (GameObject go2 in gameObjectArray2)
           {
               go2.SetActive(false);
               Debug.Log("reality2 off");
           }
           Reality2 = false;
           Debug.Log("reality1");
       }
   }
}

This turns objects tagged “layer1” off, but then never turns “layer2” active. The “layer 1” objects also don’t turn back on after pressing shift again. The debug.log triggers every time, just how I expected it to if the script worked, but the actual setactive doesn’t happen. Does anyone know the fix for this?

3 Answers

3

I think you should check the Reality2 variable to see if it switched correctly.

I am still a newbie too and encountered the same problem as yours. What I did was gave a debug log for when the input is received. When I played the game and tested the button press, the debug log came out several times instead of only one time I pressed the button.

So, it could be that your button press is taken several times in the Update() loop and because it is taken several times the switching of Reality2 variable already occurred several times.

As for the solution… I just gave up and used another button for input to do the second thing

tried this, but the input only happened once, so it seems I have a different problem

–

ah yeah, my bad, I just realized it too.. but you gave me inspiration of some sort and I double checked my code. It turns out it was because I put Input.GetKeyDown() in OnTriggerStay() function cuz I need the functions to run only when it collides with certain object.. i feel stupid but at least my problem is solved too so thanks

–

Are you sure you didn’t put the script on a GameObject with Tag “Reality1”? Because then the script would disable itself, which would explain your issue.

the gameobject i have it on is untagged

–

I fixed this! for anyone having the same issue the new script that works is

using UnityEngine;

public class LayerToggle : MonoBehaviour
{
    private GameObject[] reality1Objects;
    private GameObject[] reality2Objects;
    private bool isReality2 = false;

    private void Start()
    {
        reality1Objects = GameObject.FindGameObjectsWithTag("reality1");
        reality2Objects = GameObject.FindGameObjectsWithTag("reality2");

        // Turn off all "reality2" objects during Start. Make sure the objects with both tags are toggled active in unity editor at the start, or else this won't work
        foreach (GameObject go in reality2Objects)
        {
            go.SetActive(false);
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
        {
            Debug.Log("Shift");
            ToggleRealityLayers();
        }
    }

    private void ToggleRealityLayers()
    {
        isReality2 = !isReality2;

        foreach (GameObject go in reality1Objects)
        {
            go.SetActive(!isReality2);
        }

        Debug.Log($"Reality1: {!isReality2}");

        foreach (GameObject go in reality2Objects)
        {
            go.SetActive(isReality2);
        }

        Debug.Log($"Reality2: {isReality2}");
    }
}

Make sure both tags are toggled on in unity editor when the game starts, otherwise this won’t work. the script turns one of the tags off in start so that only one is active when the game actually starts.