- Home /
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?
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.
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) );
You are right, thanks! Let me update my previous post to prevent confuse.
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;