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.
  2. Dismiss Notice

Object Reference Not Set to the Instance of an Object

Discussion in 'Scripting' started by GamesOnAcid, Jan 11, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    280
    UPDATE: I solved the problem by setting the First Person Controller to use the FirstPersonController script, instead of MouseLook, which was included in the script already. I cleaned the code up as well. Thanks to everyone who helped and responded!


    So, I was messing around with menuing, and I came across this error on my pause menu. I got this error multiple times, but could correct is very easily. This one however, I can't decode. It tells me the error is referencing line 8 (I included the full function). My game object for character is the Unity prefab FirstPersonController. Anyone help? Thanks!


    Code (JavaScript):
    1. function PauseGame()
    2. {
    3.     savedTimeScale = Time.timeScale;
    4.     Time.timeScale = 0;
    5.     AudioListener.pause = true;
    6.     firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
    7.     mainCamera = gameObject.Find("Main Camera").GetComponent("MouseLook");
    8.     firstPersonControllerCamera.enabled = false;
    9.     mainCamera.enabled = false;
    10.  
    11.     if (pauseFilter)
    12.     {
    13.         pauseFilter.enabled = true;
    14.     }
    15.  
    16.     currentPage = Page.Main;
    17. }
     
    Last edited: Jan 12, 2016
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    For clarity, does that mean this line?
    Code (csharp):
    1.     firstPersonControllerCamera.enabled = false;
     
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    280
    Yes, firstPersonCameraController is also declared as a private variable on the top of the code.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    OK, so this means that the GetComponent is not finding the MouseLook component. Let's start by not using the string version of it - use GetComponent.<MouseLook>() instead.
     
  5. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    280
    It tells me that MouseLook does not denote a valid type. MouseLook is a script which I have in the FPSController. Also note that the error was a NullReferenceException. What is the script exactly referencing, because I gave it a value with the gameObject.Find code.
     
    Last edited: Jan 11, 2016
  6. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    AS StarManta said, remove the quotation marks from MouseLook

    7. mainCamera = gameObject.Find("Main Camera").GetComponent<MouseLook>();

    However, why would you want to name your MouseLook variable mainCamera? I think what you're trying to do is this:

    MouseLook mouseLook = gameObject.Find("Main Camera").GetComponent<MouseLook>();

    OR alternatively

    mainCamera = gameObject.Find("Main Camera");
    mouseLook MouseLook = mainCamera.GetComponent<MouseLook>();

    assuming mainCamera is of type Camera, you cannot store a class of type MouseLook

    Hope this makes sense lol (and is right :p)
     
  7. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    technically the OP is doing a name search with the "find" (ewww!! o_O), and the Camera.main function just does a tag search :D but yeah, Camera.main is a far better approach :)
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Last few replies haven't addressed the error that OP is getting now:
    I haven't looked at the Standard Assets in a while - is MouseLook a C# script? If so, there's the problem there - Javascript can't see C# classes. There's a bit of a wall in between JS and C#.

    Ultimately, the best advice I can give is to learn C# instead. First off, it's just objectively better, and second, in scenarios like this, C# classes can see other C# classes. It's less daunting than it seems - partly because Unity's scripting tutorials (all in C#) are really, really good.