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!

derHugo
  • 83,094
  • 9
  • 75
  • 115
Orion31
  • 556
  • 6
  • 28

1 Answers1

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;
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 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 '17 at 00:30