2

I am an absolute beginner. I am currently making an aim down sights script with javascript, and when aiming I want the sensitivity for both x and y to drop to 2. I tried using

GetComponent.<UnityStandardAssets.Characters.FirstPerson.MouseLook>().sensitivityX = 2;

but that doesn't work. The error for that line is

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.SetProperty (System.Object target, System.String name, System.Object value)
AimDownSights.Update () (at Assets/AimDownSights.js:33)

Does anyone know how to access the reference the Mouse Look dropdown in FPSController so I can change sensitivity? Thanks!

2
  • 1
    Do you get any error? What do you mean by "it does not work"?
    – Programmer
    Apr 26, 2017 at 4:15
  • @Programmer I edited the OP
    – Orion31
    Apr 26, 2017 at 19:30

1 Answer 1

2

First of all, you should be using C# as soon as possible because it has more features than Javascript. Also, you will get more help with C# than Unityscript. Finally, the chances of Unityscript being discontinued in the future is high so make that decision now. You can find C# tutorial for Unity here.

The MouseLook script is not not exposed in the FirstPersonController script.

We will expose the MouseLook. Open the FirstPersonController.cs script from Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts directory and add the script below to it:

public MouseLook mouseLookCustom
{
    get { return m_MouseLook; }
}

That's it. We have now exposed the MouseLook instance.


To modify the mouse stuff:

C#:

using UnityStandardAssets.Characters.FirstPerson;

public class Fpstest : MonoBehaviour
{
    FirstPersonController fps;

    // Use this for initialization
    void Start()
    {
        GameObject fpObj = GameObject.Find("FPSController");
        fps = fpObj.GetComponent<FirstPersonController>();
        fps.mouseLookCustom.XSensitivity = 5;
    }
}

Unityscript/Javascript:

#pragma strict
import UnityStandardAssets.Characters.FirstPerson;

var fps:FirstPersonController;

function Start () {
    var fpObj = GameObject.Find("FPSController");
    fps = GetComponent.<FirstPersonController>();
    fps.mouseLookCustom.XSensitivity = 5;
}
1
  • Very good answer! I started Unity this week, and since I already know Javascript, I am currently using. I have plans to learn C#, but I want to learn the feel of Unity before diving into a new language
    – Orion31
    Apr 29, 2017 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.