Accessing a static var from a different script as a GUI function

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.

If you want to access the variable from another script, you should make it public. Since the variable is static, you don’t need an instance of a GameMaster to access it, you can just access it:

GameMaster.currentScore -= 500;

Alternatively, you can make it static and private and have a public static method to set the variable.

Hi Bibi,

I’m not familiar with JavaScript/UnityScript, I use C#. But I think the general concepts are the same. So I will explain it with short C#-snippets and you hopefully see what I mean.

A static variable belongs to a class, not to a specific object created from the class-blueprint. For Example:

class Test : Monobehaviour
{
   ...
   public static int Value;
   ...
}

This class definition holds a static int named Value. To access these value from another script, simply put the class name first, then a dot-operator and then the name of the variable. In C# it looks like this:

// This is another script
...
Debug.Log("The variable named 'Value' contains: " + Test.Value);
...

Test is the name of the class (and the name of the script) where the static variable was declared!

I think/hope in JavaScript/UnityScript it works similar ?!?!?

Have a nice day …
pixelmechanic

PS: As El_Guig wrote: The static variable has to be public, otherwise you can’t access it from another place than the class itself.

Thanks El_Guig. If I add just that call to the missile GUI script like so:

The script still does not know. I get " Unknown identifier: ‘GameMaster’.". This is with the variable in the other script set to public or static, static being the one I used. I changed it and still have the same issue.

Hi pixelmechanic, thanks for the explanation. I think that is what I have done here, and by all the examples I have found it should work, but it doesn’t and I cannot understand why. I have even checked the spelling of the names several times. For whatever reason, the missile script cannot understand what GameMaster is.

GameMaster.js

MsiloClickScriptGUI.js

I didn’t make myself clear. You’re meant to leave it static and add public. They do not have the same meaning:

  • public defines the visibility of the variable: a private variable can only be accessed within the script, not outside, while a public one can be accessed from anywhere.
  • static makes the variable common to all instances of that script: if you have many scripts with “GameMaster” attached, they will share the same “currentScore” variable.
    There are many links that you can find to learn more about visibility and the meaning of static, that you can find online if you want more insight.

Having the variable set as public static var currentScore : int = 0; works, I just tested it.

Thank you again, I do appreciate the time you’ve taken to help me. Sadly I still have the same issue, following your instructions leaves me with the same error. Perhaps if I post the entire code it will help, as maybe that is part of the problem. It now looks like this:

GameMaster.js

MsiloClickScriptGUI.js

The error remains as : BCE0005: Unknown identifier: ‘GameMaster’. The scripts are on different objects in the hierarchy, but that should not matter right?

Are you sure your filename is exactly GameMaster.js?
If you remove this line GameMaster.currentScore-=500;
from MsiloClickScriptGUI.js, does the code compile?
If it does, and if you add

public var gm : GameMaster;

To the top of that file, does it still compile?

GameMaster must match GameMaster.js exactly, no extra spaces or anything, just without the .js

If I remove the line you posted then yes, it does compile correctly.

If I add the second line you posted to the MsiloClickScriptGUI.js, I get the error: BCE0018: The name ‘GameMaster’ does not denote a valid type (‘not found’).

I have checked the spelling a dozen times now, and everything matches up name wise. This is why it makes no sense. It just cannot see it in the GUI script.

Edit: Here is a screen to show the file names are correct.

I see your script is nested within a folder called Plugins, is your GameMaster script also in the plugins folder? If the MsiloClickScriptGUI.js is, the GameMaster.js script also needs to be there.

take a look at Unity - Manual: Special folders and script compilation order

Hi Bibi,

I don’t know, but do you use code in your project which wasn’t written by yourself? Maybe you have code in your project which was written in C#? When you answer both questions with YES SIR, then maybe this language mixing (C# with UnityScript/JavaScript) is part of your problem.

Take a look at the following link I found:

http://www.pepwuper.com/unity3d-beginners-tip-of-the-day-dealing-with-error-bce0018-the-name-does-not-denote-a-valid-type-unity3d/

Maybe this helps …

Have a nice day …
pixelmechanic

You are all beautiful genius people!!! Thank you ThermalFusion. That has solved the problem. I didn’t think about it because I thought it didn’t matter where the script was located - in both hierarchy and folder - when using this type of variable. I saved the script in the same folder, reassigned in the inspector and voila! I used the code lines suggested by El_Guig and perfect.

Thank you all. I will not make that mistake again.

Pixelmechanic - my current asset folders are comprised of pretty much everything I have ever looked at or downloaded from the asset store, so they are a mess of learning things, looking at things, and things I worked on. Because of this, my project folder does contain things in both script types, but I’ve been only using Javascript in my actual scenes I’m working on. I will take a look at that link. thanks again.