Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Cobrryse · Mar 15, 2014 at 02:41 PM · 2dgameobjectreferencespriterenderersprite renderer

Hide/Show GameObject from script

So here's my dilema. I am trying to display a hat (The game object is called "Party" because it is a party hat) on my character by having the player click a button in a menu that sets the hat's renderer to true. The hat is a child of my 2d character and the hat's sprite renderer's check box is unchecked (which I am assuming means that the renderer is false). When I run the game and click the button in the menu, Unity gives me an error saying "Object reference not set to an instance of an object" and the hat does not display in game. Here's my script: using UnityEngine; using System.Collections;

 public class HatSelectMenu : MonoBehaviour {
 
     private GameObject party;
     // Use this for initialization
     void Start () {
         party =  GameObject.Find("Party");
     }
     
     // Update is called once per frame
     void OnGUI () {
         if(GUI.Button (new Rect(Screen.width / 2 - 50, Screen.height /  2 + 20, 200, 75), "Show Hat"))
         {
             party.renderer.enabled = true;
             
         }
         if(GUI.Button (new Rect(Screen.width / 2 - 30, Screen.height /  2, 100, 50), "Main Menu"))
         {
             Application.LoadLevel(0);
         }
     }
 }

If you need any clarification I would be happy to help.

Comment
Add comment · Show 1
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 MousePods · Mar 15, 2014 at 03:01 PM 1
Share

I assume you know that it can't find the GameObject "Party", right?

$$anonymous$$aybe you spelled "Party" as party? :P

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by saruul34 · Mar 15, 2014 at 04:43 PM

 using UnityEngine;
 using System.Collections;
 
 public class HatSelectMenu : MonoBehaviour {
      
         public GameObject party; // Drag here party hat gameobject.
         // Use this for initialization
         void Start () {
         }
      
         // Update is called once per frame
         void OnGUI () {
            if(GUI.Button (new Rect(Screen.width / 2 - 50, Screen.height /  2 + 20, 200, 75), "Show Hat"))
            {
              if(party.activeSelf)
                 party.SetActive(true);
              if(party.renderer)
                 party.renderer.enabled = true;
              else print("Where is my hat?");
      
            }
            if(GUI.Button (new Rect(Screen.width / 2 - 30, Screen.height /  2, 100, 50), "Main Menu"))
            {
              Application.LoadLevel(0);
            }
         }
     }
Comment
Add comment · Show 5 · 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 Cobrryse · Mar 15, 2014 at 04:57 PM 0
Share

This works but it displays the buttons in the game scene. I wanted to have the buttons in my main menu of sorts...

avatar image Cobrryse · Mar 15, 2014 at 05:07 PM 0
Share

Never$$anonymous$$d my previous comment, its working

avatar image Jela · Jul 27, 2014 at 12:09 PM 0
Share

I tried this code. But it's not working. There is no error but it prints where is my hat? Why?

avatar image Exbow · Jul 27, 2014 at 03:14 PM 0
Share

cause the party.renderer.enabled is set to false?

avatar image Goldensnitch · Dec 05, 2016 at 01:56 AM 0
Share

How do i attach this to button

avatar image
0

Answer by mclankyman · Mar 15, 2014 at 03:05 PM

Check your casing. And maybe tag your hat with "Hat" and use

  party = GameObject.FindGameObjectWithTag("Hat");
Comment
Add comment · Show 1 · 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 Cobrryse · Mar 15, 2014 at 04:21 PM 0
Share

Okay, I tagged it but I still get the error mentioned in my original statement. When I click on the error Unity highlights the party.renderer.enabled = true; snippet, so I am assu$$anonymous$$g that that is where the problem is.

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

23 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

Related Questions

Need help with pressing on a sprite to put another sprite higher in the sorting order 0 Answers

Switching between 2D and 3D in the same Scene 2 Answers

Can't re-enable a gameobject 1 Answer

My sprite does not change 1 Answer

value range of Sprite sortingOrder 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