- Home /
Accessing variables from seperate scripts
I have two quick scripts I whipped up, and what I want to happen, is if a player clicks on a cube as of now, the script attached to the cube will get the int value from the other script and increment the int.
Here are my two scripts:
MouseScript.cs
using UnityEngine;
using System.Collections;
public class _MouseScripts : MonoBehaviour {
public GameObject selCircle;
public int amountOfIncreaseOfGold = 5;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
//test selection by changing color
//renderer.material.color = Color.white;
GameObject clone;
clone = Instantiate (selCircle, transform.position, transform.rotation) as GameObject;
_GUI other;
other.increaseGoldPieces(amountOfIncreaseOfGold);
}
}
And the _GUI script.
_GUI.cs
using UnityEngine;
using System.Collections;
public class _GUI : MonoBehaviour {
//Initializing the different integer variables
public int goldPieces;
public int ironOre;
public int foodStorage;
public int totalPopulation;
//String displays for on-screen
private string gold;
private string iron;
private string food;
private string pop;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
gold = goldPieces.ToString();
iron = ironOre.ToString ();
food = foodStorage.ToString ();
pop = totalPopulation.ToString ();
}
void OnGUI() {
GUI.Label (new Rect (25, 25, 100, 30), "Gold: "+gold);
GUI.Label (new Rect (25, 45, 100, 30), "Iron: "+iron);
GUI.Label (new Rect (25, 65, 100, 30), "Food: "+food);
GUI.Label (new Rect (25, 85, 100, 30), "Population: "+pop);
}
public int getGoldPieces() {
print ("It got here!");
return goldPieces;
}
public void increaseGoldPieces(int n) {
goldPieces += n;
getGoldPieces();
}
}
I've tried several variations of trying to get this to work, but I get different errors each time. I think I almost had it but I was getting a NULL exception. So I'm hoping you guys can help me out.
Thanks Chad
Answer by syclamoth · Mar 15, 2012 at 02:43 AM
Both scripts are on the same object, no? If you want to call other.increaseGoldPieces(amountOfIncreaseOfGold), you'll need to actually assign to other first.
What you are actually doing with the line
_GUI other;
is kind of like making a _GUI-shaped hole where you can stick any _GUI-type object. Without actually putting anything inside that hole, it won't work, because there's nothing there!
Assuming both scripts are on the same gameObject, you can fill that hole using this line (in between creating the reference, and calling increaseGoldPieces)
other = GetComponent<_GUI>();
GetComponent must be called on either a gameObject, or any component attached to that gameObject. It returns the first found component of the specified type (the bit between the greater-than and less-than signs) that is attached to that object. If no such component exists, it will return null- which can be another source of problems.
Of course, if the GUI script is on a different object, you'll need to somehow get a reference to that object on your cube script. There are a few ways of doing this, including GameObject.Find(string), GameObject.FindWithTag(string tag), keeping a public reference to the GUI script ahead of time using
public _GUI myGUI;
and assigning it in the inspector before runtime, and other ways (including initialisation scripts etc.). In your case, it's probably simplest to use the public variable/assign in editor method- it's the safest, and isn't difficult to implement.
Syclamoth, you covered both bases, and I should have been more specific. The scripts were on different GameObjects, and I followed your instructions exactly and it works!
Thanks for the help.