Referencing the instance of a public game object

How would one go about referencing the instance of a public game object instantiated from another script?

I have two classes here, the first is a Game Manager that only has one instance and instantiates objects as needed, the other is called Field Manager and really all it needs to do is reference an object (picked at random btw) that’s been instantiated by the game manager, so that the FieldManager Object can access that objects transform upon detecting a trigger.

As you see below I tried achieving this by using GameObject.Find (GameManager.instance.publicObject.name); but that doesn’t work. I’m still pretty new to writing code. A little help would be much appreciated.

    public class GameManager : MonoBehaviour {
    
    	public GameObject[] armyPrefabs;
    	public GameObject loadedArmyPrefab;
    	int randPrefab;
    
    	public static GameManager instance = null;
    
    	void Start () 
    	{
    		if (instance == null)
    			instance = this;
    		else if (instance != this)
    			Destroy (gameObject);
    
            randPrefab = Random.Range(0, armyPrefabs.Length);
    
            Setup();
    	}
    
        void Setup () 
    	{
    	    Instantiate (armyPrefabs[randPrefab], transform.position, transform.rotation);
    	    loadedArmyPrefab = armyPrefabs[randPrefab];
    	}
    }



    public class FieldManager : MonoBehaviour {
    	
    	GameObject currentArmy;
    
    	void Start () 
    	{
    		currentArmy = GameObject.Find (GameManager.instance.loadedArmyPrefab.name);
    	}
    
    	void Update () 
    	{
    		CheckUpdate ();
    	}
    
    	void OnTriggerEnter () 
    	{
    		if (currentArmy != null) 
    		{
                //ArmyManager.direction is just a vector3 to move currentArmy along X axis
    			currentArmy.transform.Translate (ArmyManager.instance.direction);
    		}
    	}
    
    	void CheckUpdate()
    	{
    		if (currentArmy == null) 
    		{
    			currentArmy = GameObject.Find (GameManager.instance.loadedArmyPrefab.name);
    			Debug.Log (currentArmy);
                Debug.Log (GameManager.instance.loadedArmyPrefab.name);

                //The debug above will print the instantiated prefabs name 
`               //so I'm confused... ?!?!
    		}
    	}
    }

I think your errors comes from the fact that you are correctly instantiating one of the prefabs, but you are not assigning it to loadedArmyPrefab.

The line loadedArmyPrefab = armyPrefabs[randPrefab] assigns a prefab to your variable, not the instantiated object.

What you want is to change your Setup() method like this:

void Setup () 
{
    loadedArmyPrefab = (GameObject)Instantiate (armyPrefabs[randPrefab],
                                transform.position, transform.rotation);
}

Also, both other answers are correct in that

  1. You do not need to do a GameObject.Find(); using currentArmy = GameManager.instance.loadedArmyPrefab will work just fine.
  2. You may encounter initialization errors if you do not move your GameManager initialization code from Start() to Awake()

Finally, note that (IIRC) when you instantiate a GameObject, unity will give it a name like "MyGameObject (Clone)", so trying to find it with GameObject.Find("MyGameObject") will most likely return null.

“Doesn’t work?” Unity normally gives more specific errors than that…

Most likely you’re trying to use the reference before it is assigned. Move your game manager initialisation from Start() into Awake().

I don’t get why you’re doing that weird search…

currentArmy = GameObject.Find (GameManager.instance.loadedArmyPrefab.name);

currentArmy = GameObject, loadedArmyPrefab = GameObject

THEREFORE

currentArmy = GameManager.instance.loadedArmyPrefab;

Also, NullReferenceException means that you’re trying to access a child of something that doesn’t exist. Meaning “ball.color = red”, ball doesn’t exist yet you’re trying to access it’s color.

Put more Debug.Log’s around your script to see when the game objects exist, when they don’t, what order the Start functions are being called in (You want the manager to be called first)