I have a script that is supposed to find every active object with a certain tag and on the push of a button enable and disable it's meshrenderer and collider. but the script only tries to enable and disable the previous on the object the script is attached to. what am i doing wrong and is there any feasible way to do this?
I've checked all tags, but the script keeps trying to reach this unrelated object. i even tried adding a mesh renderer and a colider. but nothing
here is the script:
private bool on = true;
public void Update()
{
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("buildnode");
if (Input.GetKeyDown("b"))
{
on = !on;
}
if (on == false)
{
foreach (GameObject go in GameObject.FindGameObjectsWithTag("buildnode"))
{
go.GetComponent<MeshRenderer>().enabled = false;
go.GetComponent<Collider>().enabled = false;
}
}
if (on==true)
{
foreach (GameObject go in GameObject.FindGameObjectsWithTag("buildnode"))
{
go.GetComponent<MeshRenderer>().enabled = true;
go.GetComponent<Collider>().enabled = true;
}
}
}
Thank you and have a good day.
if(on)
andelse
\$\endgroup\$