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... ?!?!
}
}
}