• 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 dick · Aug 06, 2011 at 09:16 PM · errornullreferenceexception

JS OOP trouble

i have a script called skills.js :

class Skill { var damage: float; var rate : float; var hits : int; var selected : boolean; var name : String;

 function Skill(nam : String, dmg : float, rat : float, hit : int, sel : boolean){
     this.damage = dmg;
     this.rate = rat;
     this.hits = hit;
     this.selected = sel;
     this.name = nam;
 } 

}

punch = new Skill("punch", 5, 0.2, 1, false); slash = new Skill("punch", 3, 0.1, 1, false); headbutt = new Skill("punch", 8, 1, 1, false); push = new Skill("punch", 2, 0.6, 1, false); ` and i want to access the it's values properties in another script. for example in player.js:

var damage = skillScript.punch.damage;
but is won't let me and it returns as a log warning when ever i start the game up. if somebody knows what i need to do please help. thanks for reading and here is the error if it helps:

NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) player.FixedUpdate () (at Assets/Scripts/player.js:54)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Aug 07, 2011 at 12:00 AM

It seems you want punch, slash etc. to be global variables. The equivalent to global in Unity is static - you must declare them this way:

static var punch = new Skill("punch", 5, 0.2, 1, false);
static var slash = new Skill("punch", 3, 0.1, 1, false);
static var headbutt = new Skill("punch", 8, 1, 1, false);
static var push = new Skill("punch", 2, 0.6, 1, false);
You can access these globals in other scripts using the script name:

  var damage = skills.punch.damage;
Comment
Add comment · Show 9 · 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 dick · Aug 07, 2011 at 01:01 PM 0
Share

thanks it works like magic

avatar image Peter G · Aug 07, 2011 at 02:31 PM 0
Share

That is actually incorrect. Static is NOT global. Static makes a variable a class variable as opposed to an instance variable. The following line is perfectly legal, but is not a global variable:

  private static var punch = ...;

only the public modifier makes a variable global (its the default in js). Static has nothing to do with the variable's accessibility level.

avatar image aldonaletto · Aug 07, 2011 at 03:03 PM 0
Share

I don't think global is the same as public: a public variable has script scope - it only exists inside the script - while the global has program scope. The definition of global variable is "a variable that has program scope", which means that it's known by everybody in the program and keeps its contents while the program exists. Isn't the same behaviour of a static variable? Ok, both may be implemented in different ways, but that's why I said "the equivalent to global in Unity is static". I really would love to know how they implement static vars - if old fashioned globals, or as properties of a commom class - but the effect is the same.

avatar image SisterKy · Aug 07, 2011 at 03:54 PM 0
Share

@Peter G Wait, are you saying the public keyword alone is sufficient to make a var a global or are you saying public + static keywords make a global?
Would you call the first of these two a global?

 public var punch = ...;
 public static var punch = ...;

So far, I had been under the impression static was enough to define a global, but I'd have no problem to learn that public static is required. But only public...? now that would break my view of the world...? :p

avatar image Peter G · Aug 07, 2011 at 10:21 PM 1
Share

@Sister$$anonymous$$y, for starters, you have to understand that Unityscript, although it has different syntax, has many similarities to C#/.Net that web js doesn't have such as strong and static typing. It also implicitly does classes as opposed to prototyping. So when I reference C#, its because that unity script behaves the same way.

C++ allowed you to create variables outside of classes which C# does not. That is a global variable in C/C++. So in a sense, you can't ever get to that scope using js or C# which is probably a good thing because it can make it difficult to debug. As @aldonaletto says, the closest you can get to this behavior is to use a public static variable which is accessible on all levels and exists for as long as the assembly of the containing class is loaded.

Around here, a lot of the time, people will refer to global variables as simply being public.

@aldonaletto, I agree with your latest comments with one small exception. I wouldn't call it script scope. It's a matter of ter$$anonymous$$ology, but its usually defined as being accessible from only within the class. The differentiation from script scope is that you can have multiple classes defined in a single file. Here's the msdn page on the private access modifier.

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Errors with Image Effects 0 Answers

Unexplanable NullReferenceException Error 2 Answers

[Closed]NullReferenceException on Object Instantiation onto game world 2 Answers

NullReferenceException 1 Answer

NullReferenceException: Object reference not set to an instance of an object - error in AdvancedBuildingSystem! 0 Answers


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