Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Accessing MouseLook in FirstPersonController Component: Unity 5

Discussion in 'Scripting' started by EternalAmbiguity, Oct 12, 2015.

  1. EternalAmbiguity


    Dec 27, 2014
    So Unity 5 seems to have dropped the separate component MouseLook to instead incorporate it into the FirstPersonController component. I had some code that relied on accessing that component, but now that it's a "sub-component" I have no idea how to access it.

    I started by trying
    Code (csharp):
    1. GameObject.FindObjectWithTag("Player").GetComponent<FirstPersonController>().GetComponent<MouseLook>().enabled = false
    --but got the "object reference not set to instance of object" error.

    I then tried what's below. This is a copy/paste of my code as it is right now.

    Code (csharp):
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using UnityStandardAssets.CrossPlatformInput;
    6. using UnityStandardAssets.Utility;
    7. using Random = UnityEngine.Random;
    8. using UnityStandardAssets.Characters.FirstPerson;
    9. ...
    10. public GameObject player;
    11. ...
    12. void Update (){
    13. ...
    14. player.GetComponent<FirstPersonController>().GetComponent<MouseLook>().enabled = !player.GetComponent<FirstPersonController>().GetComponent<MouseLook>().enabled;
    15. ...
    16. }
    Yes, I realize I don't need all of that at the top, but I copied it from the FirstPersonController script in the off-chance it was causing the issue. And there's a ton of code I didn't post because it's somewhat long and unrelated (though I certainly can if it's desired). And I realize this is almost the same, but I tried in the hopes that assigning the GameObject would somehow help unity understand where to look.

    Anyway, got the same problem. I took a look at the FirstPersonController script and saw that the MouseLook part was defined as "private" so I changed it to public--but I got the same issue. So I'm stuck here.

    Can anyone give me an example of how to access the MouseLook part of the new FirstPersonController component on the new standard "Player" GameObject? Is it a component within the component? Is it something else? I'd appreciate any help. Thanks.
  2. EternalAmbiguity


    Dec 27, 2014
    Apologies for the bump but this is something I really need some help with. I don't imagine it's a super difficult problem, I just have no idea how I'd go about fixing it. I need to know how to access the MouseLook portion of the new FirstPersonController script. When I try to do so Unity gives me the "Object reference not set to an instance of the object" error.

  3. bec_bunsen


    Mar 2, 2012
    I have the same problem but I think it actualy have access to the script.
    Just the "enabled" doesn't work.
    Coz I notice that you can still access to the sensitivityX and sensitivityY parameters.

    In my case I set it to 0 instead of disabling the script.

    Not the best solution though cause you have to re-assign the sensitivity afterward on "enabling".
  4. EternalAmbiguity


    Dec 27, 2014
    I just realized I could just disable the entire FirstPersonController. I imagine that wouldn't work in some situations, but it works for me.
  5. dmitriy-untilov


    Apr 5, 2016
    You can also edit MouseLook.cs script and add field
    public bool enabled { get; set; }
    In Init method change state to TRUE and check it state in LookRotation method.
    When you change state to FALSE from outside (for example, when you call menu) MouseLook will not be executed.

    P.S. Sorry for my english