Whenever I try to find a gameObject with GameObject.Find, I get NullReferenceException: Object reference not set to an instance of an object
NullReferenceException: Object reference not set to an instance of an object
ButtonManager.onColonyManagerClick.
Basically, whenever I click a button, I get the name of that button and try to find a gameObject with EventSystem.current.currentSelectedGameObject.name (a prefab that was Instantiated earlier)
My code:
string colonyName = EventSystem.current.currentSelectedGameObject.name;
GameObject SpecialColony = GameObject.Find(colonyName);
I can’t see all that is happening from your post. If you’re getting an exception, is it because you used SpecialColony without checking if it was null?
What line of code was the exception thrown from?
With that said, is the object of interest on the top level, or is it the child of another object?
Find doesn’t recurse through the entire hierarchy. If you don’t fashion another means of finding colonies, and must use Find, you may have to build a recursive routine which descends through all children (and children of children, on to the tail of the organization where there are no children). If you’re not familiar with creating recursive algorithms post a comment.
However, let me point out that Find is a bit problematic, in part because it doesn’t recurse. I sense a situation where you may benefit from creating a registry of colonies. That is, instead of relying upon the name of a GameObject to find colonies among loads of other objects, it may be best to create a list (List, Dictionary, etc) of the colonies, wherever they may be in the hierarchy. This makes searching faster, and limits the search to a collection colonies, instead of searching all GameObjects for a name.
You may consider the docs on GameObject find, which say if you’re looking for a child, it may be best to use Transform.Find.
The transform is actually the primary means of organization in Unity, not GameObjects.
First of all if you want better help you need to give better info
Put your code into a code block next time with ALL you code in the class,
what you’ve provided is useless for solving a possibly bigger problem.
The reason you get an error is that sometimes there is no currentSelectedGameObject
You need to check if there is before asking for data from it.
void CollectTheGarbage ()
{
var selectedObj = EventSystem.current.currentSelectedGameObject;
if (selectedObj)
{
var name = selectedObj.name;
var obj = GameObject.Find(name);
}
}
the whole way you’re going about whatever your doing is so bad its not even funny.
You get something by a string reference doesn’t even exist and then try and find something with the exact name as the object clicked (if it does work it will just return the object you clicked)
If the button and the object are the same name then how will you know if you’ve found the button again or the object?
consider putting a script on the button like this:
public class UnityAnswers : MonoBehaviour
{
public GameObject prefab;
GameObject obj;
void Start ()
{
obj = Instantiate(prefab);
}
// On the button, put this as being called onClick.
public void OnClick ()
{
// do what you want you already have a reference to the object
// (did that in start)
}
}