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!
For reference, here is the player controller script:
using System.Collections;
public class BallRollControl : $$anonymous$$onoBehaviour {
public float speed;
public Text countText;
public Text lossText;
public GameObject coinSpawn;
private Rigidbody rb;
private int count;
private CoinRandomizer script;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
coinSpawn = GameObject.Find("CoinSpawn");
script = coinSpawn.GetComponent<CoinRandomizer>();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Coin Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
script.makingCoins();
}
}
void SetCountText ()
{
countText.text = "Coins: " + count.ToString ();
}
}
Answer by Chasbas_80 · Feb 28, 2017 at 07:17 PM
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!
Thank you for the reply, but it either is not working or I am not understanding.
Your first parcel of code I added to my Player Controller Script (BallRollControl) Your second parcel of code I added to my Coin Spawn (CoinRandomizer)
Still getting errors, but I will continue to work with it.