Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Read Only Variables

Discussion in 'Scripting' started by ZeWitcher, Jul 15, 2012.

  1. ZeWitcher

    ZeWitcher

    Joined:
    Jul 13, 2012
    Posts:
    12
    Hello everyone,

    I'm new with Unity3D and I'm using JavaScript
    Is there any way that I could declare a readonly variable without using functions?
    Also, how can I add get/set functions?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You'd have to use a function. This page about JScript.NET shows how to use get/set functions (50% or so down the page, under the Productivity section), which applies to Unityscript.

    --Eric
     
  3. ZeWitcher

    ZeWitcher

    Joined:
    Jul 13, 2012
    Posts:
    12
    Oh, it looks like I'm getting errors because I'm using get/set functions out of class. Thanks! My problem is solved but I want to know why does that happen?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Because that's the way it works. ;) Other than that I don't know why.

    --Eric
     
  5. fano_linux

    fano_linux

    Joined:
    Jan 1, 2012
    Posts:
    909
    Well in C# get/set is

    private string variable;

    public string Variable
    {
    get { return this.variable; }
    set { this.variable = value; }
    }
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The question was specifically not about C#.

    --Eric