How to: find game object with tag in hierarchy and toggle mesh renderer

Hi guys,

I hope someone could help me a bit…
I am trying to find runtime instatiated prefabs (clones) of a Game Object with a specific tag and than using the Toggle to turn off (and back on) the mesh renderer for these objects…

I thought to make a empty game object and than add a script to that and than mapping the Script on the UI Toggle (On Value Changed Boolean and setting the GameObject to SetActive… but I have been only able to deactivate the object with this:

using UnityEngine;
using System.Collections;

public class Testing: MonoBehaviour
{
    GameObject[] MyTagObject;

    void Start()
    {
        Ceiling = GameObject.FindGameObjectsWithTag("MyTag");

        foreach (GameObject r in MyTagObject)
        {
            r.GetComponent<MeshRenderer>().enabled = false;
            r.GetComponent<Collider>().enabled = false;
        }

I would very much appreciate if anyone would know how to toggle the mesh renderer back on with the toggle…

any ideas?

Why don’t you show us the code that you have to enable the mesh renderer so we can see what is going wrong, i.e. what is not working?

Also, I notice that you are iterating MyTagObject, but are collecting into a variable called ‘Ceiling’.

Hi thanks for your fast reply!

yes, good point… I have changed the code afterwards…
anway I tried a new script, but it still doesn´t work…

Could you please take a look at this:

using UnityEngine;
using UnityEngine.UI;

public class ToggleObjects : MonoBehaviour
{
    Toggle m_Toggle;
    public Text m_Text;
    GameObject[] Ceiling;

    void Start()
    {
        //Fetch the Toggle GameObject
        m_Toggle = GetComponent<Toggle>();
        //Add listener for when the state of the Toggle changes, to take action
        m_Toggle.onValueChanged.AddListener(delegate {
            ToggleValueChanged(m_Toggle);
        });

     
        {
            m_Text.text = "First Value : " + m_Toggle.isOn;
            Ceiling = GameObject.FindGameObjectsWithTag("Ceiling");
            foreach (GameObject r in Ceiling)
            {
                r.GetComponent<MeshRenderer>().enabled = false;
                r.GetComponent<Collider>().enabled = false;
            }
        }
    }

   
    void ToggleValueChanged(Toggle change)
    {
        {
            m_Text.text = "New Value : " + m_Toggle.isOn;
            Ceiling = GameObject.FindGameObjectsWithTag("Ceiling");
            foreach (GameObject r in Ceiling)
            {
                r.GetComponent<MeshRenderer>().enabled = true;
                r.GetComponent<Collider>().enabled = true;
            }
        }
    }
}

any idea how to fix that?

many thanks for your support so far!

Can you be more specific what doesn’t work where and when, i.e. what you expected to see, why you expected that, and what you got instead? I see no invocation to ToggleValueChanged

A tip: instead of collecting the objects each time, why don’t you collect them into your global ‘Ceioling’ once at start, and then simply iterate over Ceiling? perhaps write a small ‘setEnable’ method like this:

public void setEnable(bool setTo) {
    foreach (GameObject r in Ceiling)  {
         r.GetComponent<MeshRenderer>().enabled = setTo;
         r.GetComponent<Collider>().enabled = setTo;
    }
}

and replace the two foreach loops in Start() and ToggleValueChanged() with an invocation to setEnable()

Edit: I belatedly see the invocation via delegate. Can you verify it invokes?

Hi,
what I am trying to achieve is simply find runtime instatiated prefabs in the hierarchy with the tag “ceiling” and than using a UI toggle to turn off and on the mesh renderer …

maybe you have an even better solution to achieve that?

What is not working, and how do you know it is not working?

it´s not activating the mesh back on

once the toggle is clicked, the mesh turns off, but it should be visible again, when the toggle is clicked again…

Does the Ceiling array hold the game objects that you expected it to hold, and does your ToggleValueChanged method get invoked, and does your log list the correct values?

Add some logging to get better info

public void setEnable(bool setTo) {
    foreach (GameObject r in Ceiling)  {
         r.GetComponent<MeshRenderer>().enabled = setTo;
         r.GetComponent<Collider>().enabled = setTo;
         Debug.Log("Trying to set " + r.name + "'s mesh and collider to " + setTo);
    }
}

Then check the log against what you expect to be set / reset

Hm I added the logging, but I have no debug msg in the console…

what has worked was this Script:

using UnityEngine;
using System.Collections;
public class Testing: MonoBehaviour
{
    GameObject[] Ceiling;
    void Start()
    {
        Ceiling= GameObject.FindGameObjectsWithTag("Ceiling");
        foreach (GameObject r in Ceiling)
        {
            r.GetComponent<MeshRenderer>().enabled = false;
            r.GetComponent<Collider>().enabled = false;
        }
    }
}

I have attached it to a game object and then mapped it to the Togle and set to active…
But ofcourse it turns the mesh renderer only to not visible, as soon as the toggle is clicked, but I would need an additional function to show the MeshRenderer from a Clone Game Object in the hierarchy (once the toggle is clicked again)

any idea how to achieve that?

Doesn’t this work?

public void setEnable(bool setTo) {
    foreach (GameObject r in Ceiling)  {
         r.GetComponent<MeshRenderer>().enabled = setTo;
         r.GetComponent<Collider>().enabled = setTo;
         Debug.Log("Trying to set " + r.name + "'s mesh and collider to " + setTo);
    }
}

Or do you have issues with getting invoked?