Skip to main content
added 131 characters in body
Source Link
Jim U
  • 263
  • 1
  • 11

This sets GameObjects' visibility by name by activating/deactivating them:

using System.Collections.Generic;
using UnityEngine;

public class Utility : MonoBehaviour
{
    static Dictionary<string, GameObject> gameObjects;

    public void Awake()
    {
        gameObjects = new Dictionary<string, GameObject>();
        foreach (var o in Resources.FindObjectsOfTypeAll<GameObject>())
            if (!gameObjects.ContainsKey(o.nameCompareTag("MyTag"))
                gameObjects.Add(o.name, o);
    }
    
    static public void SetVisible(string name, bool visibility = true)
    {
        gameObjects[name].SetActive(visibility);
    }
}

This makes the game object named "AnitaBath" visible by activating it

Utility.SetVisible("AnitaBath");

This makes the game object named "AnitaBath" invisible by deactivating it

Utility.SetVisible("AnitaBath", false);

How to Use This Script

This script should be added to a unique object in your scene like GameManager. It enumerates all objects in the scene tagged "MyTag" whenever it's Awake() method is called.

It assumes GameObject names are unique, you've created a new tag called "MyTag" and added this tag to the objects you'll be calling SetVisible on.

If you don't use the tag, you run the risk of affecting hidden internal objects (e.g., "SceneLight", "InternalIdentityTransform", "PreviewMaterials", "Cone", "Cube", ...)

This sets GameObjects' visibility by name by activating/deactivating them:

using System.Collections.Generic;
using UnityEngine;

public class Utility : MonoBehaviour
{
    static Dictionary<string, GameObject> gameObjects;

    public void Awake()
    {
        gameObjects = new Dictionary<string, GameObject>();
        foreach (var o in Resources.FindObjectsOfTypeAll<GameObject>())
            if (!gameObjects.ContainsKey(o.name))
                gameObjects.Add(o.name, o);
    }
    
    static public void SetVisible(string name, bool visibility = true)
    {
        gameObjects[name].SetActive(visibility);
    }
}

This makes the game object named "AnitaBath" visible by activating it

Utility.SetVisible("AnitaBath");

This makes the game object named "AnitaBath" invisible by deactivating it

Utility.SetVisible("AnitaBath", false);

This sets GameObjects' visibility by name by activating/deactivating them:

using System.Collections.Generic;
using UnityEngine;

public class Utility : MonoBehaviour
{
    static Dictionary<string, GameObject> gameObjects;

    public void Awake()
    {
        gameObjects = new Dictionary<string, GameObject>();
        foreach (var o in Resources.FindObjectsOfTypeAll<GameObject>())
            if (o.CompareTag("MyTag"))
                gameObjects.Add(o.name, o);
    }
    
    static public void SetVisible(string name, bool visibility = true)
    {
        gameObjects[name].SetActive(visibility);
    }
}

This makes the game object named "AnitaBath" visible by activating it

Utility.SetVisible("AnitaBath");

This makes the game object named "AnitaBath" invisible by deactivating it

Utility.SetVisible("AnitaBath", false);

How to Use This Script

This script should be added to a unique object in your scene like GameManager. It enumerates all objects in the scene tagged "MyTag" whenever it's Awake() method is called.

It assumes GameObject names are unique, you've created a new tag called "MyTag" and added this tag to the objects you'll be calling SetVisible on.

If you don't use the tag, you run the risk of affecting hidden internal objects (e.g., "SceneLight", "InternalIdentityTransform", "PreviewMaterials", "Cone", "Cube", ...)

added 131 characters in body
Source Link
Jim U
  • 263
  • 1
  • 11

This sets GameObjects' visibility by name by activating/deactivating them:

using System.Collections.Generic;
using UnityEngine;

public class Utility : MonoBehaviour
{
    static Dictionary<string, GameObject> gameObjects;

    public void Awake()
    {
        gameObjects = new Dictionary<string, GameObject>();
        foreach (var o in Resources.FindObjectsOfTypeAll<GameObject>())
            if (!gameObjects.ContainsKey(o.name))
                gameObjects.Add(o.name, o);
    }
    
    static public void SetVisible(string name, bool visibility = true)
    {
        gameObjects[name].SetActive(visibility);
    }
}

This makes the game object named "AnitaBath" visible by activating it

Utility.SetVisible("AnitaBath");

This makes the game object named "AnitaBath" invisible by deactivating it

Utility.SetVisible("AnitaBath", false);

This sets GameObjects' visibility by name by activating/deactivating them:

using System.Collections.Generic;
using UnityEngine;

public class Utility : MonoBehaviour
{
    static Dictionary<string, GameObject> gameObjects;

    public void Awake()
    {
        gameObjects = new Dictionary<string, GameObject>();
        foreach (var o in Resources.FindObjectsOfTypeAll<GameObject>())
            if (!gameObjects.ContainsKey(o.name))
                gameObjects.Add(o.name, o);
    }
    
    static public void SetVisible(string name, bool visibility = true)
    {
        gameObjects[name].SetActive(visibility);
    }
}

This makes the game object named "AnitaBath" visible by activating it

Utility.SetVisible("AnitaBath");

This makes the game object named "AnitaBath" invisible by deactivating it

Utility.SetVisible("AnitaBath", false);

This sets GameObjects' visibility by name by activating/deactivating them:

using System.Collections.Generic;
using UnityEngine;

public class Utility : MonoBehaviour
{
    static Dictionary<string, GameObject> gameObjects;

    public void Awake()
    {
        gameObjects = new Dictionary<string, GameObject>();
        foreach (var o in Resources.FindObjectsOfTypeAll<GameObject>())
            if (!gameObjects.ContainsKey(o.name))
                gameObjects.Add(o.name, o);
    }
    
    static public void SetVisible(string name, bool visibility = true)
    {
        gameObjects[name].SetActive(visibility);
    }
}

This makes the game object named "AnitaBath" visible by activating it

Utility.SetVisible("AnitaBath");

This makes the game object named "AnitaBath" invisible by deactivating it

Utility.SetVisible("AnitaBath", false);
Source Link
Jim U
  • 263
  • 1
  • 11

This sets GameObjects' visibility by name by activating/deactivating them:

using System.Collections.Generic;
using UnityEngine;

public class Utility : MonoBehaviour
{
    static Dictionary<string, GameObject> gameObjects;

    public void Awake()
    {
        gameObjects = new Dictionary<string, GameObject>();
        foreach (var o in Resources.FindObjectsOfTypeAll<GameObject>())
            if (!gameObjects.ContainsKey(o.name))
                gameObjects.Add(o.name, o);
    }
    
    static public void SetVisible(string name, bool visibility = true)
    {
        gameObjects[name].SetActive(visibility);
    }
}

This makes the game object named "AnitaBath" visible by activating it

Utility.SetVisible("AnitaBath");

This makes the game object named "AnitaBath" invisible by deactivating it

Utility.SetVisible("AnitaBath", false);