I’ve decided to switch to C# and am going to be asking some retarded newb questions if you don’t mind
transform.localEulerAngles.x = 10;
that line ^^
gives me this:
Assets/Interdimensional Jesus Velociraptor/scripts/playerControl.cs(15,27): error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.localEulerAngles’. Consider storing the value in a temporary variable
Unfortunately this is a minor inconvenience that just has to be worked around. ‘localEulerAngles’ and other accessors like it are implemented as properties (presumably). Since Vector3 is a struct, a copy is returned by the property (not a reference to the field), so when you try to assign a value to the ‘x’ field, you’re actually trying to modify the temporary copy that was returned.
The workaround, more or less, is to write something like this:
transform.localEulerAngles = new Vector3(
10f,
transform.localEulerAngles.y,
transform.localEulerAngles.z
);
If this is something you’re doing often, you could probably write some convenience functions to clean things up a bit.
Edit: this post does not contain any more information than Jesse’s post
Technical explanation (skip if you don’t care :P):
localEulerAngles and most other members of transform is a property. A property looks like a normal variable, but it is actually a function. This particular property returns a Vector3. Vector3 is a struct, which means that it always returned as a copy. All this means that the property localEulerAngles returns a copy of the rotation. The error is the compiler protecting you from assigning this value to a useless copy. If it didn’t give the error, the code would do nothing.
Solution:
Always assign a full vector3 to the property:
It doesn’t have to be a new vector3, but in C# you always need to assign a Vector3. Technically you can also use any of the Translate, Rotate, RotateAround or LookAt functions of Transform, but I don’t think that that’s what you’re asking ;).
Another question about this. Is there anyway to look at the actual ‘transform.position’ or ‘transform.eulerAngles’ function that gets called?
or is there anywhere that documents everything this function does in C# specifically?
and again thank you for the technical explanation, I do want to know whats going on behind the scenes of the code
Also, I’m not exactly following the use of the ‘new’ tag in c#. In JS you could just do blah = Vector3(1,2,3). But it seems doing = Vector3(1,2,3) anything in C# gives an error and the ‘new’ tag is always need, whats the technical reason for that? Was JS automatically making a ‘new’ vector3 for something to be equal to without the need for the new tag to be there?
No, you cannot view the source of these functions (except by buying a source code licensed version of Unity, which is probably not very realistic :P).
Not really.
You could get some generic idea of how such a thing could work by reading up on matrix transformations and quaternion math, but that does not give you information about what the specific functions do as they might simply be returning cashed values.
New is obligatory by design in C#. Whenever you construct an object through one of its constructor functions, you need to use new. In JS, new is optional. JS will add it for you if you omit it. This is basically the key selling point of Unity’s JS: it implements several workarounds to shorten code for things that are rather verbose in C# (like the issue from your initial post in this thread).
When I said you don’t need to add a new vector3, what I meant to say was that you could also use a vector you obtained from somewhere else, for example: