• 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
Question by microp · Apr 24, 2011 at 01:22 AM · errortypecompileconstant

The type 'UnityEngine.Vector3' cannot be declared const

Hello! It's been a w$$anonymous$$le since I last programmed in C#, and I've run into t$$anonymous$$s error when compiling one of my scripts:

The type 'UnityEngine.Vector3' cannot be declared const

Here is the code that is causing it:

public const Vector3 up = new Vector3(0, 0, 1);

I wasn't aware of any C# mechanic that could allow specific types to explicitly prevent the const specification. I can't find anyt$$anonymous$$ng on that matter either on the forums, Answers, nor Google. Would anyone care to enlighten me? I wish there was an error code database I could look into.

Thanks!

Comment
Mike 3
Macarse
SaSha_K
ZOM585
divinesense
$$anonymous$$

People who like this

6 Show 0
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
Best Answer

Answer by Mike 3 · Apr 24, 2011 at 01:31 AM

From http://msdn.microsoft.com/en-us/library/ms173119.aspx

Only the C# built-in types (excluding System.Object) may be declared as const.

You may want to look into readonly instead - very similar, but allows writing at construction time

Comment
Eric5h5
Macarse
FWCorey
McGravity
Teku-Studios
Necronomicron
SaSha_K
$$anonymous$$

People who like this

8 Show 3 · 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 microp · Apr 24, 2011 at 02:11 AM 0
Share

Wow. Coming from a C++ background, I find that quite puzzling. Anyway, thanks a lot!

avatar image microp · Apr 24, 2011 at 02:30 AM 0
Share

Unless there's something else I missed, "readonly" values don't seem like they work with default parameters though, do they?

avatar image Mike 3 · Apr 24, 2011 at 02:57 AM 0
Share

const has a different meaning in c# to c++, but at any rate, no, you can't use readonly for default parameters, but you can't use vectors for them at all anyway

avatar image

Answer by PatHightree · May 27, 2011 at 12:14 PM

If you change your field into a property with only a getter, you will get the desired behavior.

 public Vector3 up { get { return new Vector3(0, 0, 1); } }
Comment
Wolfram
Berenger
FWCorey
cmberryau
guneyozsan
mkusan
Ziplock9000

People who like this

5 Show 7 · 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 Wolfram · May 27, 2011 at 01:30 PM 0
Share

This is the way to go.

avatar image ProtoTerminator · Oct 09, 2016 at 08:41 PM 2
Share

This isn't the best approach, as it will create a new Vector3 every time the property is called. A better approach using the property method would be to make a private setter and set it in the class's constructor or Awake function.

 public Vector3 up { 
     get;
     private set;
  }
avatar image Ziplock9000 · Jul 25, 2017 at 01:13 PM 0
Share

This misses the whole point of trying to use a constant if it's going to be calculating and creating a new Vector 3 each time.

avatar image Bunny83 Ziplock9000 · Jul 25, 2017 at 02:01 PM 3
Share

No, this is like a true constant. Vector3 is a value type. So any time you "pass around" a Vector3 it is copied anyways. new Vector3 does not "create" anything. It just initializes the value type. It doesn't allocate any memory. Vector3.up is literally implemented like this:

 // copied from decompiled UnityEngine.dll
 public static Vector3 up
 {
     get
     {
         return new Vector3(0f, 1f, 0f);
     }
 }

How the actual native code will look like might depend on the order of optimisation but a value type constructor like this is most likely inlined. So in the end there would be no real difference between using new Vector3 or just assigning the value from another variable.

You can even initialize value types manually as long as you initialize all fields:

 Vector3 v;
 v.x = 0;
 v.y = 1;
 v.z = 0;
 // use v here

avatar image Tortuap Bunny83 · Feb 22, 2022 at 02:56 PM 0
Share

Sadly even trivial properties are hardly inlined : see https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity8.html, chapter "Trivial properties".

avatar image _dns_ · Jul 25, 2017 at 02:09 PM 2
Share

@ProtoTerminator @Ziplock9000 Vector3 is a value type and will be created on the stack to be returned to the caller, not allocated on the heap and then not being garbage collected later. The only thing that happens is the copy of the 3 floats of the Vector3 struct, nothing else (well, there is still a function call that would not be here for a simple type like an int or a float, might be optimized by the compiler though). The "new" syntax can be misleading with value type.

EDIT: arf, @Bunny83 is always faster to reply... I guess I'll have to change name for Turtle83 :)

avatar image Bergquist · Sep 15, 2020 at 03:03 PM 0
Share

This answer is not correct.

The compiler will not accept this as a compile time constant, and cannot be used in replacement of a const where a const is required, for example as an optional parameter.

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

`ActivateTrigger.target' is a `field' but a `type' was expected 1 Answer

DEBUG;TRACE; Compile Problems 1 Answer

typedefing in unity 1 Answer

Android compile error? 1 Answer

InvalidCastException: Cannot cast from source type to destination 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