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 /
avatar image
0
Question by BHKeWin · Aug 01, 2019 at 02:41 PM · hideshow

How do you hide objects in Unity2D,

I want to hide some objects in a scene (more specifically when i press the options button on the main menu i want quit, play, options to disappear and back to appear) My code looks like this :

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Options : MonoBehaviour { public GameObject QuitB; public GameObject OptionsB; public GameObject PlayB; public GameObject BackB;

 void OnMouseDown()
 {
     QuitB.SetActive(false);
     OptionsB.SetActive(false);
     PlayB.SetActive(false);
     BackB.SetActive(true);
 }

} But the buttons won't disappear and they still react to clicking. Is there any way to fix this? Or is there any way to hide an object? I've been searching for over 2 hours but nothing seems to work. I'm a Newbie i just started learning Unity and C#.

EDIT : What fixed my is that i was using prefabs instead of GameObjects and it turns out you can't "hide" prefabs with specific commands that can hide GameObjects THANK YOU ALL!

Comment
Add comment · Show 7
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 I_Am_Err00r · Aug 01, 2019 at 02:57 PM 0
Share

Did you assign those buttons in the inspector?

avatar image BHKeWin I_Am_Err00r · Aug 01, 2019 at 03:25 PM 0
Share

They look pretty assigned to me.alt text

helpunity.png (8.0 kB)
avatar image TreyH · Aug 01, 2019 at 03:30 PM 0
Share

Are you sure that's getting called? What is the object holding your Options component?

avatar image BHKeWin TreyH · Aug 01, 2019 at 03:51 PM 0
Share

It's the options button object.

avatar image Bonfire-Boy BHKeWin · Aug 01, 2019 at 04:14 PM 0
Share

What about the other question?

Are you sure that's getting called?

If unsure, add logging to the function

Show more comments

2 Replies

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

Answer by Vega4Life · Aug 01, 2019 at 04:03 PM

From what it looks like, and based on what was said. They are linking the prefabs of the buttons, not the actual ones in the scene. Probably because they are instantiating them in code, so they just need to get reference to the ones created - not the prefabs.

Comment
Add comment · Show 8 · 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 BHKeWin · Aug 01, 2019 at 04:05 PM 0
Share

All of the Button objects have Box colliders which have "is trigger" enabled

avatar image Vega4Life BHKeWin · Aug 01, 2019 at 04:11 PM 0
Share

If you create a cube, put a script on with:


     private void On$$anonymous$$ouseDown()
     {
         Debug.LogError("TEST");
     }


It will show up in the logs when you click the cube in the game scene (not the editor scene).

avatar image BHKeWin Vega4Life · Aug 01, 2019 at 04:16 PM 0
Share

Yeah the Debug commands work with it including Debug.LogError, Debug,Log , Debug.LogWarning

Show more comments
avatar image
1

Answer by I_Am_Err00r · Aug 01, 2019 at 03:42 PM

Ok, last guess here, where are you calling OnMouseDown()? Do you have something like this:

 private void Update()
 {
     if(Input.GetMouseButtonDown(0))
      {
            OnMouseDown();
      }
 }

Because if you are just declaring void OnMouseDown, Unity doesn't know that means to call that if you are pressing the primary mouse button (or whatever you have it assigned to); you need to have unity check for when you press that and then call the function.

Comment
Add comment · Show 6 · 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 BHKeWin · Aug 01, 2019 at 03:52 PM 0
Share

I'm guessing there must be a problem with this scriptalt text

script.png (19.6 kB)
avatar image I_Am_Err00r BHKeWin · Aug 01, 2019 at 04:05 PM 0
Share

So I was wrong, turns out this is a standard unity event that can be called, but has some restrictions for it to work: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDown.html


Try adding an update that does what I said and then maybe change the name from On$$anonymous$$ouseDown() to something like Clicked(), here is an example:

 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
 public class Options : $$anonymous$$onoBehaviour 
 { 
     public GameObject QuitB; 
     public GameObject OptionsB; 
     public GameObject PlayB; 
     public GameObject BackB;
     
     private void Update()
  {
      if(Input.Get$$anonymous$$ouseButtonDown(0))
       {
             Clicked();
       }
  }
     
      void Clicked()
      {
          QuitB.SetActive(false);
          OptionsB.SetActive(false);
          PlayB.SetActive(false);
          BackB.SetActive(true);
      }
 }

Let me know if that works.

avatar image BHKeWin I_Am_Err00r · Aug 01, 2019 at 04:13 PM 0
Share

The button's down in the "projects" windows disappear from their preview but they can still be seen on the "game" tab. Also if i end the testing of the game the buttons disappear like they were deactivated like i want them to. But for some reason i still see them in-game. Could this be because i add them to the scene with the *.SetActive(); command? video : https://youtu.be/YR-6BB5AWSU

Show more comments
avatar image Vega4Life · Aug 01, 2019 at 04:04 PM 0
Share

Unity does detect this - its a built in method. It just needs to be on a UI element or something with a collider.

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

110 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

Related Questions

Creating Objects and Hiding when out of Range 1 Answer

Hide/show multiple 3d models at once ,Hide/Show Multiple 3D Models at once 0 Answers

How do i make a texbox show and hide 1 Answer

show-hide some GUItexts in one script 1 Answer

show gui when look certain object 2 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