An instance of type X is required to access non static member Y [javascript]

Having problem with getting this to work. The two variables at the bottom are thowing the errors (one for each variable used)

var MapTerrainSizeX : int = 1 ;
var MapTerrainSizeY : int = 1 ; //Total terrain size.  Everything generated happens in here.
var MapImageDiv : int = 1 ; //How big the image size of the game is.  
var MapImageBorder : int = 1; //How thick the game border is.  Players can not move past this, however game occurs naturally from inside such as spawns
var GameTerrainData : TerrainData = null; //Uses premade terrain data.  Required.

static var GameImageSizeX : int = MapTerrainSizeX / MapImageDiv - 1;
static var GameImageSizeY : int = MapTerrainSizeY / MapImageDiv - 1;

static var GameImagePixel  = MultiDim.FloatArray(GameImageSizeX + 2, GameImageSizeY + 2); //Contains the power of pixels.  Most sampling done here.
static var GameImageReduce = MultiDim.FloatArray(GameImageSizeX + 2, GameImageSizeY + 2); //Reduce map.  0.01-1 = Regular, does not sample onto others.  1.01 - 2 = sampled pixels gain property.  1 = regular
static var GameImageStay   = MultiDim.FloatArray(GameImageSizeX + 2, GameImageSizeY + 2); //How many passes before pixel is sampled.  0-99 = regular, does not pass to others.  100-200 = passes value, stops at 100.  

Now it is these two variables causing what I presume is the trouble.

static var GameImageSizeX : int = MapTerrainSizeX / MapImageDiv - 1;
static var GameImageSizeY : int = MapTerrainSizeY / MapImageDiv - 1;

You are having your static variables reference a non-static variable. You can not do this; static variables can only access other static variables.

To build off Daniel's answer, if you need your static variables to be static so you can access them easily, and need the other variables to be non-static because you're modifying your instance of the object, consider using the singleton pattern.

In c#, a good way to implement it is here: You could do basically the same thing in Javascript.

Basically you get rid of your static variables (as you can't access non-static variables with them), and make a static instance of your class to access your instance's variables.

So your client code would look something like `MyClassName.Instance.GameImageSizeX` instead of `MyClassName.GameImageSizeX`;

Use this instead:

static var GameImageSizeX : int;
static var GameImageSizeY : int;

function Start () {
    GameImageSizeX = MapTerrainSizeX / MapImageDiv - 1;
    GameImageSizeY = MapTerrainSizeY / MapImageDiv - 1;

That doesn't actually have to be in Start (or Awake), but it's generally a good idea to have variables outside functions be declared for type only; anything resembling "actual code" should be inside functions.

What if what we need is to acces a NON static variable from another JAVASCRIPT ?


var VidaMinaB : int = 20 ;

function Update() {
if (VidaMinaB <= 0) {
Instantiate (ExplosioMina, transform.position , Quaternion.identity);

(then this value is changing since it is the trap’s health (its destructible), I made a script which instances an damage area, and I want this gamobject (damage area) knows when the trap’s healt is 0 in order to destroy it… ; but if i set the variable VidaMinaB as static, then , the damage area script doesnt receive the new value of VidaMinaB, and if the trap is desroyed the damage area remains there…)

im calling the variable in the other script like this :


var VIDAMINAB : int ;

function Update () {

if (VIDAMINAB <= 0) {
Destroy(this.gameObject , 0.5);

I sopose there is some way to access this “int” that is NON STATIC of antoher JAVASCRIPT … if i set the variable nonstatic i only get the Error posted on the title of that page

And finally, the other error is: when I destroy 1 trap, all the other traps are destroyed too… , I watch the value VIDAMINAB of the damage areas created and is still 20, even when the traps are destroyed…

I’d higly appreciate your help, thank you very much in advance.

Have a nice day

Angel M.