Beginner Help: Making a Script Public for GetComponent Call on another script

I am just starting out with Unity and have a question about making a script public. I have the following code:

using UnityEngine;
using System.Collections;

public class CoinRandomizer : MonoBehaviour {

	public Transform[] spawnlocations;
	public GameObject[] whatToSpawnPrefab;
	public GameObject[] whatToSpawnClone;

	void Start()
	{
		makingCoins ();
	}

	void makingCoins(){
		whatToSpawnClone [0] = Instantiate (whatToSpawnPrefab [0], spawnlocations [0].transform.position, Quaternion.Euler (0, 0, 0)) as GameObject;
		whatToSpawnClone [0] = Instantiate (whatToSpawnPrefab [0], spawnlocations [0].transform.position, Quaternion.Euler (0, 0, 0)) as GameObject;
		whatToSpawnClone [0] = Instantiate (whatToSpawnPrefab [0], spawnlocations [0].transform.position, Quaternion.Euler (0, 0, 0)) as GameObject;
	}
}

…I have set up another script dictating when this script should run (the other script runs when your player object makes contact with a coin, so this script runs to generate the next coin, see script in comments), but I need to make “makingCoins” a public variable for the other script to call / access.

Simply adding “public makingCoins” to the top of the code doesn’t work. Help!

@ErikReichenbach

Hello, I believe that you need to get a reference to your CoinRandomizer class so you can access the makingCoins() function from your other class. The code would look something like this:

 using UnityEngine;
 using System.Collections;
     
     public class OtherClass : MonoBehaviour {
    
         public CoinRandomizer coinRandomizer;

  void Start()
    {
          coinRandomizer = FindObjectOfType<CoinRandomizer>(); 
    }

Then, use dot operator to access the function from the CoinRandomizer class:

coinRandomizer.makingCoins();

This should do the trick. Let me know if it works for you and good luck!