Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

NullReferenceException help 1 Answer

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

Phantom / ghost reference throwing null reference error 0 Answers

" NullReferenceException: Object reference not set to an instance of an object" 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges