Skip to main content
2 of 3
added 131 characters in body
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);
Jim U
  • 263
  • 1
  • 11