Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Aubrey-Falconer · Dec 17, 2011 at 11:37 PM · javascriptstaticsingleton

Variable Accessors in UnityScript (Get{} and Set{})

I am (finally) learning the nuances of the Singleton design pattern, and was wondering if there's any way to use a variable accessor (get{} or set{}) in UnityScript. The c# syntax is quite simple and can be found all over the internet, but no matter what I try, I can't get it to work in JavaScript.

Here's some (pseudo)code to turn a class called WhirldIn into a singleton hosted by a GameObject to enable certain methods it contains to yield. It would work fine if I used a static var called S to store the singleton reference and a static function to initialize the var, but it just seems so much cooler to call WhirldIn.S.Load and have the singleton initialize itself if necessary :)

 private static var s : WhirldIn = null;    //Static Singleton Reference
 static function get S () : WhirldIn {    //Could be replaces with an init function
     if (s == null) {
         Debug.Log("Initializing WhirldIn GameObject");
         var go : GameObject = new GameObject();
         s = go.AddComponent(WhirldIn);
         go.name = "_WhirldIn";
         DontDestroyOnLoad(go);    //Ensure the GameObject host of this Singleton class survives scene changes
     }
     return s;
 }

Also, is there any practical overhead in always calling a .S accessor rather than just using a .S static var after initializing the singleton manually?

Here's some relevant threads I have been able to find:

http://answers.unity3d.com/questions/140946/c-enums-get-and-set-accessor-expected.html (Talks about a "get and set accessor in UnityScript")

http://www.unifycommunity.com/wiki/index.php?title=Singleton (C# Singletons)

http://forum.unity3d.com/threads/59621-Static-getter-setter (Discussion on how to make this work - but the solution gives me compilation syntax errors)

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
2
Best Answer

Answer by Bunny83 · Dec 18, 2011 at 12:08 AM

From what i've heared it's not possible to create static properties in UnityScript.

Read "Mike 3"'s answer on this question:

Edit : Fixed the link

Comment
Add comment · Show 8 · 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 Aubrey-Falconer · Dec 18, 2011 at 01:04 AM 0
Share

Hmm yes - his answer does seem to address my situation directly. Strangely enough though, when I copy his code into my script: var moo = 1; function get $$anonymous$$oo() : int { return moo; } function set $$anonymous$$oo(value : int) { moo = value; } I get this error on the middle (get) line: "expecting EOF, found 'function'.". Not sure what's up with that...

avatar image Eric5h5 · Dec 18, 2011 at 02:14 AM 0
Share

@Aubrey Falconer: you need all of the code, not just the code inside the class declaration. Note what $$anonymous$$ike 3 said about "explicitly declared classes".

avatar image Aubrey-Falconer · Dec 18, 2011 at 08:29 AM 0
Share

Heh, interesting. After I got his example to compile with your tip, the warning about static properties turned out to be completely correct. I sure wish Unity would work harder to discourage anyone from learning UnityScript until it achieves feature parity with C#... Was already planning to convert all my projects to C#, here's one more good reason to do so!

avatar image jahroy · Dec 18, 2011 at 09:03 AM 0
Share

I don't think there's any reason to discourage anybody from using UnityScript. It's a wonderfully simple language that has never let me down once. That's nice that you like C#, but I have never needed it at all.

It would be great if you could help me understand the advantage of using properties. I've asked why people use them a couple times and have never gotten an answer. I also don't understand why there would be a need for static properties or what is wrong with using a static function in your example.

avatar image Aubrey-Falconer · Dec 19, 2011 at 02:54 AM 0
Share

I have been a VERY happy UnityScript user for the past three years - I have written thousands and thousands of lines of code in it. Now that I am beco$$anonymous$$g a better programmer however, I wish that someone had made me start out in C# to begin with :) There are of course coders far more talented then myself who swear by UnityScript, but I am just giving my personal experiences here. When you start trying to do things like initialize a multidimensional array (for working with raw terrain data, for example) UnityScript just won't cut it anymore, and you will begin to get the urge to transition to the more powerful C#.

The example given on this page is honestly the only good use I have ever encountered for properties, and it has to be static in order to be useful:

I have a library which loads game worlds off of the internet, and provides resources for those worlds once they are loaded. It works perfectly fine for me to manually initialize the library (having it create a static reference for itself once initialized) when my application starts - but it would be far cooler for me to not worry about initializing the library, and ins$$anonymous$$d have it initialize itself automatically the instant it is needed.

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Compile priority - accessing static variables 1 Answer

Static singleton is (not nullref -empty-) but assigned to "null" in the other Scenes 2 Answers

Extending a Singleton base class (UJS) (?) 2 Answers

When to create singleton and static objects, 0 Answers

Cannot use Static var/ Unknown Idientifier 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