• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ben_the_Lad · Apr 26, 2015 at 09:38 PM · nullreferenceexceptionpauseinstanceobject reference

Pause Script Suddenly Stops Working

Hello,

I had a simple pause script (adapted from this tutorial) that I built last night. It worked very well and was 100% reliable. Tonight, it seems to have broken, though I've made no changes to the script (or any other script, for that matter).

The script is here:

 #pragma strict
 
 private var pauseGame : boolean = false;
 private var showGUI : boolean = false;
 
 function Update()
 {
     if(Input.GetButtonDown("Pause"))
     {
         pauseGame = !pauseGame;
 
         if(pauseGame == true)
         {
             Time.timeScale = 0;
             pauseGame = true;
             (GameObject.Find("Main Camera").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
             (GameObject.Find("First Person Controller").GetComponent("MouseLook") as MonoBehaviour).enabled = false;
             showGUI = true;
         }
     }
 
     if(pauseGame == false)
     {
         Time.timeScale = 1;
         pauseGame = false;
         (GameObject.Find("Main Camera").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
         (GameObject.Find("First Person Controller").GetComponent("MouseLook") as MonoBehaviour).enabled = true;
         showGUI = false;
     }
 
     if(showGUI == true)
     {
         (gameObject.Find("PausedText").GetComponent("Text") as MonoBehaviour).enabled = true;
         (gameObject.Find("PausedImage").GetComponent("Image") as MonoBehaviour).enabled = true;
         (gameObject.Find("Crosshair").GetComponent("Image") as MonoBehaviour).enabled = false;
         (GameObject.Find("Main Camera").GetComponent("Blur") as MonoBehaviour).enabled = true;
     }
 
     else
     {
         (gameObject.Find("PausedText").GetComponent("Text") as MonoBehaviour).enabled = false;    
         (gameObject.Find("PausedImage").GetComponent("Image") as MonoBehaviour).enabled = false;
         (gameObject.Find("Crosshair").GetComponent("Image") as MonoBehaviour).enabled = true;
         (GameObject.Find("Main Camera").GetComponent("Blur") as MonoBehaviour).enabled = false;
     }
 }

Now, when I run the game, timeScale goes to 0, but the other functions (disabling MouseLook and toggling the GUI stuff) do not work.

However, if I turn on my PauseGUI by default, it will render (even though it should not per this script) and turn off the first time Escape is tapped, then be unresponsive.

I get this error in the console:

 NullReferenceException: Object reference not set to an instance of an object
 Pause.Update () (at Assets/Scripts/Pause.js:26)
 

The only thing that changed was that I downloaded and imported the Survival Shooter demo, played with it for a few minutes, then deleted it when I discovered how badly it mucked up my folder structure. I did not delete any of the files that this one interacts with (Mouselook and blur are still there) nor did I alter the shape of my prefab.

So... does anyone have any thoughts? Cause I'm at a loss. :D

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Calum1015 · Apr 27, 2015 at 01:56 AM 0
Share

you need to declare variables. You would do it as so:

var g : gameObject;

 function Update()
 {
 g = gameObject.Find("yourGameObjectGoesHere").GetComponent<yourComponentHere>() as $$anonymous$$onoBehaviour.enabled = true; // or false
 }
avatar image Ben_the_Lad · May 04, 2015 at 02:28 AM 0
Share

Unfortunately, the code you suggested (and a few permutations) did not solve the issue.

I rewrote the script from scratch, first the tutorial and then my own personal modifications, and it works again. It looks identical; I'm not sure what changed. However, all is well. :) Thanks for your suggestion.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Gameobject.find & my error NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

NullReferenceException help 1 Answer

How to call a function from another script. 0 Answers

what the heck do you put in the object reference!? all i need is a variable! 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges