How to access variables from an instanced prefab script in another script?

I have two scripts of relevance. One is attached to a prefab of an asteroid which is spawned many times during gameplay. The other is a game controller script which is in the hierarchy.
Here is the code for the asteroid:

public class n_BoulderScript : MonoBehaviour 
	public float maxSpeed;
	public float minSpeed;
	public float amountToMove;
	public float tumbleSpeed;

	void Start()

		amountToMove = Random.Range(minSpeed, maxSpeed);
		rigidbody.angularVelocity = Random.insideUnitSphere * tumbleSpeed;


	void FixedUpdate()

		rigidbody.velocity = new Vector3 (amountToMove, 0.0f,0.0f);



I want to be able to change the variables “maxSpeed” and “minSpeed” in the asteroid script from within the following script but I don’t know how because the asteroid prefab does not exist in the hierarchy until the game begins.

Here is the gamecontroller script:

public class n_GameControllerScript : MonoBehaviour {

	public float boundaryTop;
	public float boundaryBottom;
	private float spawnPosition;
	public int hazardCount;
	public GameObject hazard1,hazard2,hazard3;
	public float spawnWait;
	private int hazardModel;
	public float waveWait;

	void Start () 


	void Update ()
		hazardModel = Random.Range (1,4);


	   IEnumerator SpawnWaves()
		while (true) 

			for (int i = 0; i < hazardCount; i++) 
					spawnPosition = Random.Range (boundaryBottom, boundaryTop); // this is the z coord
					GameObject asteroidInstance= (GameObject)Instantiate (hazard1, new Vector3 (18.2f, 6f, spawnPosition), Quaternion.identity);
					yield return new WaitForSeconds (spawnWait);

			hazardCount ++;
			yield return new WaitForSeconds (waveWait);


			} // while loop ends

	}  // ienum ends
} // end brack

For clarity, the first script is called n_BoulderScript and the second is called n_BoulderScript.

Please answer in c#. I am a beginner at programming in unity, I have tried looking at other answers to the same question but I can’t understand them at all.


You instantiate a new asteroid at line 36. So, asteroidInstance is your game object coming from the prefab. It’ll have your script attached, so you can use GetComponent() to get the script, then change the variables you want.