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