- Home /
Ref modifier
I have a script that is accessing the rotation of the camera from my MouseLook script, but it is telling me to add the red modifier. However when this is added it tells me to remove the ref modifier, is it retarded or something ?
Anyway, here is the code that is causing all of the problems:
using UnityEngine;
using System.Collections;
public class GunScript : MonoBehaviour {
public GameObject cameraObject;
private float targetXRotation;
private float targetYRotation;
private float targetXRotationV;
private float targetYRotationV;
public float rotateSpeed = 0.2f;
public float holdHeight = -0.5f;
public float holdSide = 0.4f;
void Start () {
}
void Update () {
transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * new Vector3(holdSide, holdHeight, 0));
targetXRotation = Mathf.SmoothDamp(targetXRotation, camera.transform.GetComponent<MouseLook>().rotationX, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation, camera.transform.GetComponent<MouseLook>().rotationY, targetYRotationV, rotateSpeed);
}
}
Here is a picture of the console: (Lines 20 & 21 are the targetXRotatation and targetYRotation in the update loop)
after adding the ref the console looks like this:
Any help is greatly appreciated.
Answer by Landern · Oct 12, 2014 at 12:55 PM
After adding it(to the wrong parameter) it told you that it's in the wrong position(you added it to the wrong parameter), the ref needs to be added to the THIRD parameter of the method SmoothDamp
void Update () {
transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * new Vector3(holdSide, holdHeight, 0));
targetXRotation = Mathf.SmoothDamp(targetXRotation, camera.transform.GetComponent<MouseLook>().rotationX, ref targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation, camera.transform.GetComponent<MouseLook>().rotationY, ref targetYRotationV, rotateSpeed);
}
NOT
void Update () {
transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * new Vector3(holdSide, holdHeight, 0));
targetXRotation = Mathf.SmoothDamp(targetXRotation, ref camera.transform.GetComponent<MouseLook>().rotationX, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation, ref camera.transform.GetComponent<MouseLook>().rotationY, targetYRotationV, rotateSpeed);
}
To hit this home further, you should have gotten the error in MonoDevelop or whatever IDE you're using before even going back to Unity.
But lets consider the following fake code.
using UnityEngine;
using System.Collections;
public class SomeClass : MonoBehaviour
{
public float a = 0.1f;
public float b = 0.2f;
public float c = 0.4f;
public float d = 0.5f;
// Use this for initialization
void Start ()
{
var d = Mathf.SmoothDamp(a, b, c, c);
}
}
This code would throw the following exception in Unity Editor:
error CS1502: The best overloaded method match for
UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid arguments If i put the ref in front of the second parameter: > error CS1502: The best overloaded method match for
UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid argumentserror CS1615: Argument
#2' does not require
ref' modifier. Consider removingref' modifier Exception CS1615 and CS1502 are instructing you, the developer, that #1, you have not met the method signature(the parameters do not match the method being called), #2 that you added a ref modifier to the wrong parameter. Lets remove it from parameter #2 and add it to #4: > error CS1502: The best overloaded method match for
UnityEngine.Mathf.SmoothDamp(float, float, ref float, float)' has some invalid argumentserror CS1620: Argument
#3' is missing
ref' modifier
A little bit different, but is LITERALLY telling you in this case that it's missing on parameter #3.
Lets add it to parameter #3.
No Exceptions/Errors
[1]: /storage/temp/33581-noref.png
Your answer
Follow this Question
Related Questions
Mover Player With seesaw 1 Answer
Unity and Custom Defines 1 Answer
C# Scripts don't work in Unity 3.5 any idea why? 1 Answer
Why won't this compile? 1 Answer