Accessing static variables

Hi again :slight_smile:

I’m creating a minimap system that toggles to use the entire screen or just a small portion.

My code is contained in MinimapCamera.js which is assigned to a GameObject of the same name. Now I need to access this information from other scripts, namely my movement and AI ones.

Some code:

in MinimapCamera.js:

static var mapOpen : boolean = false;

...

function Update () {

    if(Input.GetButtonDown("MinimapToggle")) {
        toggleMinimap();
    }

...

function toggleMinimap() {
    if(mapOpen) {
        camera.rect = Rect (0,0,1,1);
        mapOpen = true;
        Screen.lockCursor = true;
    }
    else {
        camera.rect = Rect (0.8,0.8,1,1);
        mapOpen = false;
        Screen.lockCursor = false;
    }
}

Then, in the standard FPS walker script:

function Update() {
    if (grounded  !MinimapCamera.mapOpen) {
...

However, this throws an error in the console:

Assets/Standard Assets/Scripts/FPSWalker.js(12,26): BCE0005: Unknown identifier: ‘MinimapCamera’.

Any advice would be much appreciated.

I would do this: Change the name of MinimapCamera and try it with another name just to make sure spelling is right or code is wrong. :smile:

Unfortunately, this occurs regardless of the script and variable names.

It looks like the JS script it not available at the FPS script.

This usually happens if you’re trying to access your JS script form a C# script (which I guess is not your case), or if one of the scripts is into a folder that compiles before the other.

For instance, create a “Plugins” folder and put your script camera there and give it another try.

That’s an odd fix, but it works. Cheers! :slight_smile:

Is there a guide anywhere as to what order scripts become accessible in?

–Eric

Awesome, that makes a lot more sense now. Guess I should rtfm more in future!

Is it really good to know what is compiled first ? Everything will be done when doing Ctrl + S…? And if anything error comes, it will be shown in the console.

It is.

Because if you have a C# script and a JS script in the same folder, the C# script will be compiled first.

Therefore, while the C# is being compiled, the JS is not yet compiled, so that JS is, as far as the C# script is concerned, non existent.

I hope that makes sense :slight_smile:

If there are references to the Javascript file in the C# file = errors, because it is not compiled yet ?

That’s correct.

And what if there are references of C# files, in Javascript files ? The inverse also, in many scripts ? A giant mess to remember what to compile first ?

http://www.unifycommunity.com/wiki/index.php?title=MinimapCamera

Slightly off the new discussion but I’ve been through and hopefully explained what I’m doing.

Random test build here, but I’m not quite used to web player configuration just yet so there are a few errors. M for minimap toggling, WASD + mouselook for standard view, and then point/click (thanks to the amazing A* pathfinding system!) navigation in the map view. The mouse scroll wheel zooms in and out on the map.