Hi all, thanks for taking the time to read.
I am trying to access a static var of integer type from a script that does not contain it’s definition. I have looked at the manual and various examples, but having followed them for different things, does not work in my situation.
So I have a GUI element that adds money over time to the player. The currentScore is the static variable I am trying to access elsewhere. This works as I want it too.
#pragma strict
static var currentScore : int = 0;
var offsetY : float = 40;
var sizeX : float = 100;
var sizeY : float = 40;
function Start () {
while (true) {
yield WaitForSeconds(155);
currentScore += 6000;
yield WaitForSeconds(10);
}
}
function OnGUI () {
GUI.Label (new Rect (Screen.width/2-30, offsetY, sizeX,sizeY), "Credits: " + currentScore);
}
Ok, so on my GUI script for firing a missile, I want to access this variable on the GUI click, but no matter how I define it, it either doesn’t know what GameMaster is, or doesn’t know what currentScore is. This is what I am with so far. As I said, I looked at GetComponent, but the example doesn’t work.
if (GUI.Button (Rect (10,110,180,80), "Target Mid Range")) {
var clone1 : Rigidbody;
clone1 = Instantiate(missile, transform.position, transform.rotation);
clone1.velocity = transform.TransformDirection (Vector3.up * 50);
clone1.transform.Rotate(-90,0,0);
audio.PlayOneShot(sound);
var script : GameMaster;
script = GetComponent ("currentScore");
script.currentScore -=500;
The idea being that when a missile is fired, it removes ‘credits’ from the players score, the end result being I want to stop them from being able to be fired once the score is below what is required, but I haven’t made it that far yet. Any help with accessing the script component would be great. I know most people here are probably sick to death of simple questions, so thanks for taking a look.
Edit: To be clear, the clone and missile themselves all works, I just need help calling the variable from a different script. I’ve also tried
but still it doesn’t know what GameMaster is.
