• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by windexglow · Jul 12, 2010 at 06:38 PM · javascriptvariablestaticinstancebce0020

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;

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by uplay1km · Jul 28, 2016 at 01:24 PM

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

TRAP'S SCRIPT

var VidaMinaB : int = 20 ;

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

(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 :

DAMAGE AREA SCRIPT

var VIDAMINAB : int ;

function Update () { VIDAMINAB = FISICAMinaBARRERA.VidaMinaB;

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Eric5h5 · Jul 12, 2010 at 07:00 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Tetrad · Jul 12, 2010 at 06:55 PM

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: http://answers.unity3d.com/questions/10696/singletons-with-coroutines/10701#10701 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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Daniel 6 · Jul 12, 2010 at 06:44 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jul 12, 2010 at 07:02 PM 0
Share

Only when declaring them. Once static variables are declared, they can access non-static variables with no problems.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Problem setting up an array with a size determined by a variable 2 Answers

Accessing another var on another script 1 Answer

Trivial question for Javascript/Unityscript experts: static variables? 4 Answers

Global variables - static keyword ? UnityScript? javascript? 1 Answer

Unknown identifier, OnCollisionEnter Error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges