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
0
Question by JekasG · Nov 15, 2014 at 02:46 AM · c#variablenumberfixed

Using fixed numbers

I was just wondering how to make a never changing number when called at the start of the script ?

So once you have defined it from the Awake() or Start() how can i make it so the number is fixed ? Even functions can't change it.

I dont want to provide a definite number, like 2 or 5 or 100 or even 1000000 I want to make a variable so i can change it in the Awake() or Start() function. And keep it fixed throughout the whole script.

Is that possible ?

Thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jeff-Kesselman · Nov 15, 2014 at 02:52 AM

NO.

There is no magic to Awake or Start, they are simply methods called by Unity for you.

If they can modify the number, so can any other class method.

Having said that you COULD create a class that holds a value that can only be set once by anyone. It would look something like this:

 class OneTimeFloat {
    private float _value;
    private bool beenSet=false;
 
    public float Value {
        get { return _value;}
        set {
            if (!beenSet){
                _value = value;
                beenSet=true;
            } else {
                throw new Exception("Attempt to reset value of OneTimeFloat");
            }
       }  
 
    }
 }
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
1

Answer by Bunny83 · Nov 15, 2014 at 03:09 AM

The C# language allows two different ways of defining constants. The first and most obvious is using the "const" keyword. A field that has the const keyword is actually a compile-time constant. So when the code is compiled everywhere you used the constant it will insert the value of that constant.

Const fields can only be initialized when they are declared:

     public const int myConstant = 5;

They can't be changed at runtime as they actually aren't fields at all. const fields are automatically static.

The second way is using the "readonly" keyword:

     public readonly int myReadOnlyField = 20;

A readonly field is an actual field in a class, but it can only initialized from a field initializer at declaration or from a constructor when the class is created. The languages doesn't allow to change the value any time after the constructor has been called.

So you can't use Awake or Start to initialize readonly fields. In Unity MonoBehaviour classes readonly fields behave the same as a const field.

Note: As i said readonly fields can't be changed due to a language restriction. With reflection it actually is possible to change it at runtime since it's an actual field in the class. However, DON'T DO THIS, EVER! readonly fields are meant to not change their value.

The only way to build a construct that allows setting a value once in Awake or Start is using a class like the one @jeff-kesselman posted.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Calling a variable in C#? 1 Answer

Variable is always false... 1 Answer

Is it possible to use a variable as a Type? 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