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 boxing_rex · Oct 12, 2014 at 12:39 PM · c#

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)

http://i.imgur.com/3RnfRvT.png

after adding the ref the console looks like this:

http://i.imgur.com/b9pcTB4.png

Any help is greatly appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 arguments

error CS1615: Argument #2' does not require ref' modifier. Consider removing ref' 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 arguments

error 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


noref.png (3.1 kB)
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

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Looking around without the entire body moving? 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