Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 17, 2018 at 05:36 PM by eskelinentopi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by eskelinentopi · Apr 16, 2018 at 06:27 PM · uigameobjectbuttonnullreferenceexceptionsetactive

NullReferenceExeption on gameObject.SetActive

Alright, again I have to seek rescue from the forum... So, this time I'm trying to set UI objects (buttons, dropdowns) visible/invisible, but I keep getting the NullReferenceExeption error. Got no idea why this would be, anybody do? Here's my code:

 public class RestartScript : MonoBehaviour {
 
     public MeshCreator meshCreator;
 
     void Start () {
 
         gameObject.SetActive (false);
     }
 
     void Update () {
 
         if (meshCreator.roundStarted) {
 
             gameObject.SetActive (true);
         }
     }
 
     public void Restart () {
 
         int scene = SceneManager.GetActiveScene().buildIndex;
         SceneManager.LoadScene(scene, LoadSceneMode.Single);
     }
 }
 

I have put in the reference in the inspector, but it doesn't seem to be working.alt text

Also, the error comes from line 13, where it says

 if (meshCreator.roundStarted) {
         gameObject.SetActive (true);
 }

imageedit-2-7436765681.png (8.7 kB)
Comment
Add comment · Show 3
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 TreyH · Apr 16, 2018 at 06:30 PM 0
Share

Which line is it complaining about?

avatar image Bunny83 · Apr 16, 2018 at 09:11 PM 1
Share

You don't have included your whole script so the line numbers are off. What exact line does the error come from?

ps: your logic seems to be flawed here. You deactivate the gameobject in Start which will completely turn off this gameobject. That means no Update callback will be executed anymore. The script can never activate itself if it's not already active. This makes no sense.


If you get a NullReference exception, please include the whole stacktrace and the exact line.

avatar image melsy · Apr 17, 2018 at 02:58 AM 0
Share

Bunny083 has mentioned it already and it is correct. There is no reference because this script will never run. Start will turn it iff then Update will never run. What you need to do is create a second script and GameObject that will manage this script and turn it on and off outside of it. A manager script to handle all of the stuff like this in the level. A Game$$anonymous$$anager is very common for stuff like this and is highly recommended.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by shadowpuppet · Apr 16, 2018 at 09:22 PM

I tried this simple script below and it didn't work because once I set the gameObject to false, the script stops running. I didn't get any errors though, it just didn't work. The script is on the gameObject you are setting to false just as you have it

 void Update(){
     
         if (Input.GetKeyDown (KeyCode.A)) {
             gameObject.SetActive (false);
             Debug.Log ("off");
         }
          
         if (Input.GetKeyDown (KeyCode.D)) {
             gameObject.SetActive (true);
             Debug.Log ("on");
         }
         } 
 
     }
Comment
Add comment · Show 4 · Share
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 eskelinentopi · Apr 17, 2018 at 04:38 AM 1
Share

So basically I need to control the true/false mechanism from outside the ones that need to be set? That does make a lot of sense though.

avatar image shadowpuppet eskelinentopi · Apr 17, 2018 at 12:48 PM 1
Share

No, what doesn't make a lot of sense is trying to run a script on a gameObject that is, in effect, non existent from the Start. you can pretty much use the same script but make the the gameObject you want to make false public, and put the script somewhere else

avatar image eskelinentopi shadowpuppet · Apr 17, 2018 at 05:36 PM 1
Share

Alright, thanks. $$anonymous$$anaged to make it work now :D A silly mistake that was.

Show more comments

Follow this Question

Answers Answers and Comments

167 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

one button to hide and unihed gameobjects 0 Answers

Unity ads , can't we show video by clicking a button? 1 Answer

How can I set active a random Ui object from a list on a button press then reset it after the button pressed? 1 Answer

Issue with spawning buttons and assigning listeners 1 Answer

Make a gameobject inactive and active 0 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