Finding Game Objects and Components in Unity with a Simple Pattern
A simple pattern for people who want to avoid the headache of Finding Game Objects and Components by searching for them.
I have some fun here for those who search for game objects and components in the scenes using Unity:
GameObject.Find("") , FindObjectOfType<> , GetComponent<> Or any other searching method. And since searching for objects and components is a high cost operation and annoying for some people. This is a very simple and easy pattern that will make the process easier.
We start with making a class StaticObject, which holds the game object, its name, and a reference to its components that we will need to use.
And then we create a class AllStaticObjects, which holds a list of the StaticObject to assign in the inspector, and an indexer to search for the desired game object by its name.
Second step is creating a class StaticObjectComponent, which holds a component and its name. Then a class AllComponents, which holds a list of, StaticObjectComponent to assign in the inspector, and an indexer to search for the desired component by its name.
Of course we don't forget to mark all the classes as
[System.Serializable] So that they be displayed in the inspector.
In our manager script, we make a singleton, and a public reference to AllStaticObjects class to assign them from the inspector. Now, if we want to access a game object we can just type it like this:
GameObject ui =(GameObject)StaticObjectsManager.Instance.AllObjects["UI Canvas"].TheGameObject;
if we want to access a component, we will do:
var animation = (Animation)StaticObjectsManager.Instance.AllObjects["UI Canvas"].AllComponents["Animation"].Component;
This is the full code.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class AllStaticObjects
{
public List<StaticObject> Objects;
public StaticObject this[string name]
{
get
{
foreach (StaticObject staticObject in Objects)
{
if (staticObject.Name == name)
{
return (staticObject);
}
}
throw (new MissingReferenceException("The game object with name -" + name + "- is not found"));
}
}
}
[System.Serializable]
public class StaticObject
{
public string Name;
public GameObject TheGameObject;
public AllComponents AllComponents;
}
[System.Serializable]
public class AllComponents
{
public List<StaticObjectComponent> Components;
public StaticObjectComponent this[string name]
{
get
{
foreach (StaticObjectComponent component in Components)
{
if (component.Name == name)
{
return (component);
}
}
throw (new MissingReferenceException("The component with name -" + name + "- is not found in the game object " + name));
}
}
}
[System.Serializable]
public class StaticObjectComponent
{
public string Name;
public Component Component;
}
public class StaticObjectsManager : MonoBehaviour
{
public static StaticObjectsManager Instance;
#region Singleton
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
#endregion
#region Game Objects
public AllStaticObjects AllObjects;
#endregion
}
Recommended resources
Game Engines
Unreal Engine 5 Best Practices
Amazon · Book
Written by multi-award-winning Unreal generalist Tyson J. Butler-Boschma, Founder and Creative Director of Toybox Games Studios, this book addresses common challenges you face when advancing your expertise in lighting, environment design, and cinematic storytelling.
GameDev.net may earn a commission if you purchase through these links. This helps fund the site at no extra cost to you.
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion