(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?
tried this, but the input only happened once, so it seems I have a different problem
– DevZillaah 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
– swarasasti