- Home /
The type 'UnityEngine.Vector3' cannot be declared const
Hello! It's been a while since I last programmed in C#, and I've run into this 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 anything 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!
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
Wow. Co$$anonymous$$g from a C++ background, I find that quite puzzling. Anyway, thanks a lot!
Unless there's something else I missed, "readonly" values don't seem like they work with default parameters though, do they?
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
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); } }
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;
}
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.
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
Sadly even trivial properties are hardly inlined : see https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity8.html, chapter "Trivial properties".
@ProtoTer$$anonymous$$ator @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 :)
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
Follow this Question
Related Questions
Error with scripts compiling Unity4. 5 Answers
NotSupportedException: Microsoft.CSharp.CSharpCodeProvider::.ctor error 0 Answers
Unsupported int type Vector3f ERROR 2 Answers
Missing headers in XCode project generated by Unity 1 Answer
Fix "Type size exists in both System.drawing and netstandard" 1 Answer