Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Static properties in UnityScript?

Discussion in 'Scripting' started by Vapid-Linus, Aug 26, 2015.

  1. Vapid-Linus

    Vapid-Linus

    Joined:
    Aug 6, 2013
    Posts:
    64
    For various reasons outside of my control I have to use UnityScript rather than C#. I have gotten non-static properties to work fine in UnityScript but not static ones.

    I used to have this code:
    Code (JavaScript):
    1. public static var currentType : int = 0;
    But then I wanted a hook to when this value is changed, so I tried this:
    Code (JavaScript):
    1. private static var _currentType : int = 0;
    2. public static function get currentType() : int { return _currentType; }
    3. public static function set currentType(value : int) {
    4.     _currentType = value;
    5.     Debug.Log("Selected type: " + _currentType);
    6. }
    But when I compile and some object tries to call ClassName.currentType I get this error:
    Anyone know why this doesn't work?
     
  2. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Well, UnityScript has not been updated recently as far as I can tell. You should really switch to C# or JavaScript.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    @Eric5h5 might have a clue. My UnityScript is too weak. It looks how I would expect a static property to appear.

    If you need a quick hack you could always wrap it in an instance member. Its dodgy, but it should work.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity doesn't use Javascript and never has. The choices are Unityscript, and C#. Neither have been updated in years, so telling people to switch doesn't help anyone.

    Anyway, as far as I know properties were never fixed to support static types. You'd have to use standard functions instead; that's what properties basically do anyway.

    --Eric
     
  5. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    Actually, If you go into the Unity Documentation, the only options for your code type are C# and JScript. there is no option for UnityScript in the docs. And Unity does use JavaScript. C# HAS been updated numerous times, I know. I have been using C# in Unity for 4 years and it has updated a few times. If it has never been updated, how can we use the newest C# methods if the version you say Unity has does not support them?
     
  6. Deleted User

    Deleted User

    Guest

    We can't use the newest C# methods. o_O There are so many things not available in Unity's C# like dynamic or async / await, null propagation, etc.
     
    Kiwasi likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Pipe down before you hurt yourself.

    UnityScript is the community's name for the language Unity calls JavaScript. They are the same thing. UnityScript is preferred because it makes it clear that the language is not ECMA JavaScript that you would use in web development.

    It's also true that niether language has been actively upgraded by Unity for several years. Unity's version of C# is about equivalent to 3.5. Considering the language is up to 6.0 now, that's a fair few versions behind.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    C# in Unity hasn't been updated since somewhere around Unity 2, so you're quite mistaken. It's way behind the current C# used in .NET. If you'd ever written actual Javascript code, you'd know that Unity doesn't use Javascript, no matter what they might choose to call it.

    Anyway this is all completely off-topic.

    --Eric
     
    Kiwasi likes this.
  9. Code to Reality Studios

    Code to Reality Studios

    Joined:
    Aug 25, 2015
    Posts:
    64
    I have written in Java script. I started with it with Unity. If it is not supported, why is it an option?
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not. They called it "Javascript", probably for marketing reasons, but Unityscript is not Javascript. You don't think simple Unityscript code like "var foo : int = 5" would work in Javascript, I hope. Calling C# "Java" wouldn't somehow magically turn C# into Java, either. But like I said, this is all off-topic, so further messages along these lines in this topic will be deleted. You can start another topic if you want to discuss this further.

    --Eric
     
    Kiwasi and BenZed like this.