Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 top2323 · Jun 19, 2018 at 03:47 AM · script errorfunction call

Unable to access function in different script

Hey there, I have a gun script and a separate camera script. The gun script is supposed to pass a quaternion to the camera script, which is then added (in a function) to the camera's rotation. However I get "component does not contain a reference for rotateCam()"

Here's where I reference the camera script. It's the mouseLook variable.

 private Component mouseLook;
 
     // Use this for initialization
     void Start () {
 
         muzzle = transform.Find("Muzzle");
         anim = GetComponent<Animator>();
         cam = Camera.main.transform;

         mouseLook = cam.GetComponent<MouseLook>();

         aimHelper = cam.GetChild(0);
         hipHelper = cam.GetChild(1);
 
 
         anim.speed = animSpeed;
 
         rotRecoilOffset = Quaternion.Euler(0, 0, 0);
     }


Here's the bad line (further down in the above script)

         mouseLook.rotateCam(ctargetRot);

And here's the function in the camera script:

   public void RotateCam (Quaternion ctargetRotation)
     {
         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
 
         md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
         mouseLook += smoothV;
 
         mouseLook.y = Mathf.Clamp(mouseLook.y, minView, maxView);
 
 
         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right) * ctargetRotation;
         character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
     }


Any ideas?

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
1
Best Answer

Answer by Feelnside · Jun 19, 2018 at 04:31 AM

Make sure that the Camera has the MouseLook script. Try to change

  private Component mouseLook;

to the:

  private MouseLook mouseLook;

Add the verification before the call:

 Debug.Log("is it not equal to null? " + (mouseLook != null));
 mouseLook.rotateCam(ctargetRot);

So that if we get "True" in the console than we can be sure that there is no problem with the reference.

Even if the problem exists, try to assign this component in the inspector. For example, change the following line:

 [SerializeField] MouseLook mouseLook;

And assign MouseLook in the inspector. In case if it will not help, we should take a look at an another place in the code.

Comment
Add comment · Show 4 · 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 Bunny83 · Jun 19, 2018 at 07:31 AM 1
Share

Note that he's calling rotateCam while the method is named RotateCam.


Also your debug log statement doesn't work in it's current form. Addition takes precedence over equality. Therefore you will first combine your string with the mouseLook variable into a new string and then compare the string against null which will always be just "true". You need to place brackets

 Debug.Log("is it not equal to null? " + (mouseLook != null) );

avatar image Feelnside Bunny83 · Jun 19, 2018 at 10:44 AM 0
Share

You are right, thanks! Let me update my previous post to prevent confuse.

avatar image DawidNorasDev Bunny83 · Jun 19, 2018 at 11:45 AM 0
Share

Those are the correct answers. 2 things you need to change: 1. method is named differently, than you call it. (RotateCam and rotateCam) 2. you are storing $$anonymous$$ouseLook Component in mouseLook variable of Type`Component` This is right thing to do. But then when you write mouseLook. compiler thinks that you are trying to make a method call on a`Component` object. and`Component` doesn't have method named RotateCam. you need to tell compiler that this is a $$anonymous$$ouseLook object by changing field declaration to private $$anonymous$$ouseLook mouseLook;

avatar image top2323 · Jun 19, 2018 at 02:31 PM 0
Share

Thanks a bunch, that's done it

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

87 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 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 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

calling a function from a script on a collsion object 1 Answer

Call a function on a script attached to an object 1 Answer

(WebGL) Activate Unity Functions with HTML/Javascript 1 Answer

Function being called on multiple objects when just one is clicked 1 Answer

Error on compiling unity headlookController. 0 Answers


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