3
\$\begingroup\$

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

its tries to reach the builder even though there is no tag attached to it

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Looking at this script it should work. The only explanations which would come to my mind is that the objects don't actually have the right tag or that another script interferes somehow. \$\endgroup\$ – Philipp Mar 29 '19 at 23:55
  • \$\begingroup\$ i edited the question to show in depth about whats happening sorry i couldn't just add the comment. is there an explanation for the above? \$\endgroup\$ – Indiepixel Mar 30 '19 at 0:47
  • \$\begingroup\$ It looks like you probably have a different object called "builder" in your scene that's causing these problems. \$\endgroup\$ – DMGregory May 3 '19 at 11:59
  • \$\begingroup\$ Two other things: I'd move your two if-blocks inside the key-down check (there is no need to turn things on or off every frame! Nothing's changed! ) and I'd use if(on) and else \$\endgroup\$ – Draco18s no longer trusts SE May 3 '19 at 14:35
0
\$\begingroup\$

It seems like this code should work, you probably need to add checks to make sure the gameObject is active, and has the components you want to work with. do this with a Linq where clause like this:

  GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("buildnode")
     .Where(go => go.activeInHierarchy && go.getComponent<MeshRenderer>() && 
     go.getComponent<Collider>()).ToArray();

then you can really simplify the if statements by just removing them:

foreach(var go in gameObjectArray){
     go.GetComponent<MeshRenderer>().enabled = on;
     go.GetComponent<Collider>().enabled = on;
}
\$\endgroup\$
0
\$\begingroup\$

You cant find Disabled Objects with GameObject.FindGameObjectsWithTag("buildnode")

you can find object you want using:

  GameObject[] items=     Resources.FindObjectsOfTypeAll(typeof(UnityEngine.GameObject)) as GameObject[];
  GameObject obj = items.FirstOrDefault(x=>x.tag=="mytag");
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.