Trying to access object created via script.

I have a ‘GameManger’ script that spawns an object at run-time… lets call it MySphere. (the reason for it being created at runtime could vary… perhaps diffuculty settings, user settings… etc)

So, I am wanting to be able to access this object and its components *(it’s position, scripts it has, etc)*somehow by other scripts.

For a start, in the GameManager script I tried:

public Transform MySphere
void Start () {
    Instantiate(MySphere,new Vector3(0,0.5f,0), Quaternion.identity);
}

But if I try to access, say, MySphere.Transform.position even in the same script, it will constantly return (0,0,0)… why is this? The object was Instantiated at (0.0,0.5,0.0) !

Another thing I tried was, in a seperate script on a seperate object in the scene:

void Start () {
    mySphere = GameObject.FindWithTag("ShufflePlayer");
}

Which always returns NullReferenceException.

Similar if I try to access it’s script:

private MySphereScript sphereScript;
void Start () {
    sphereScript = (MySphereScript)GameObject.FindWithTag("TheSphere").GetComponent(typeof(MySphereScript));
}

So it compiles fine, but at runtime it’s another NullReferenceException.

Cheers!

MySphere is your prefab. The return value of Instantiate is a reference to the new GameObject.