I am trying to access a script on another gameobject that is instantiated in my script, and no matter what I do the script always returns null.
I instantiate the gameobject first by doing this.
asteroid = (GameObject)Instantiate(asteroidPrefab);
after that I try acessing its script by doing this:
AsteroidHandler handler = (AsteroidHandler)asteroid.GetComponent(typeof(AsteroidHandler));
To check whether or not the handler was set properly I did this:
if(handler == null){
Debug.Log("Asteroid Handler is Null");
}else{
Debug.Log("Asteroid Handler was setup properly");
}
The problem is this always does the null option. I have the AsteroidHandler script attached to the asteroidPrefab prefab, and when I Instantiate it, it Instantiates properly, as I see the asteroid on the screen. Still when I try accessing the AsteroidHandler script that is attatched to it, it seems to act like it isn't there;
I have tried a couple other methods to access AsteroidHandler but they all say its null:
AsteroidHandler handler = (AsteroidHandler)asteroid.GetComponent<AsteroidHandler>();
.
AsteroidHandler handler = (AsteroidHandler)asteroid.GetComponent(typeof(AsteroidHandler)) as Asteroid Handler;
I have looked at a couple other questions similar to mine, but none of the answers seem to help.
Any help would be appreciated.