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
2
Question by Flowered · Feb 08, 2015 at 07:40 PM · hideshow

Show and Hide a button when object is clicked per mouse

Hello,

I have a button I cannot hide (it is always on Screen but I want to hide it and then Show it) I have a houseobject and if i click on this house I want my button do appear on the Screen.

my script on the houseobject so far is:

 void OnMouseDown ()

 {
     Debug.Log ("houseclicked");

 }


Shall I parent the button to the houseobject?

Thanks for reading, cheers

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 Mmmpies · Feb 08, 2015 at 08:13 PM 0
Share

Is your button the new 4.6 GUI or an old OnGUI button?

avatar image Flowered · Feb 08, 2015 at 10:16 PM 0
Share

It is a new 4.6 GUI Button. Do you know how to hide it until I click on the houseobject to let it appear on Screen ?

Thank you

avatar image AcE_fLoOdEr · Feb 09, 2015 at 03:40 AM 0
Share

@Flowered Disable/Enable the Canvas in which the button is in. Or, create a script with a public Button object, drag and drop the button gameObject from the hierarchy, and then do what you need to do.

2 Replies

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

Answer by Mmmpies · Feb 10, 2015 at 09:05 AM

Hi @Flowered,

Sorry I missed that you'd responded to me. Assuming this is the only button on the Canvas do this. As this is the new UI...

Add a CanvasGroup to the Canvas the button is on and un-tick interactable and blocksRaycast also set alpha to 0. The button is now invisible.

In the script where you click the house and then want the button to appear alter it to this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class asdf : MonoBehaviour {
 
     public CanvasGroup myCG;//Its your Canvas
 
     void OnMouseDouwn()
     {
         Debug.Log("Pressed left click.");
         myCG.interactable(true);
         myCG.blocksRaycast(true);
         myCG.alpha = 1;
     }
 }

Or just set the Canvas to active:

     using UnityEngine;
     using UnityEngine.UI;
     using System.Collections;
     
     public class asdf : MonoBehaviour {
     
         public Canvas myCanvas;//Its your Canvas
     
         void OnMouseDouwn()
         {
             Debug.Log("Pressed left click.");
             myCanvas.SetActive(true);
         }
     }

If you want the canvas active but just want to hide the button then you can use the CanvasGroup method but just put the CanvasGroup on the button itself.

There are lots of options for doing this so don't feel you have to stick with what I put I think really you just need to learn how to access the components on the new UI.

EDIT

And this @Flowered is why I shouldn't type stuff on my phone!

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class MenuOnClick : MonoBehaviour {
         
     public Canvas myCanvas;//Its your Canvas
     
     void OnMouseDown()
     {
         Debug.Log("Pressed left click.");
         myCanvas.gameObject.SetActive(true);
     }
 }

Put this on your house object and put your Canvas in the slot for the script and make sure you un-tick the Active button on the canvas:

CanvasNotTicked

That should work for you.

EDIT 2

and if you want to use the CanvasGroup method:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class MenuOnClick2 : MonoBehaviour {
     
     public CanvasGroup myCG;//Its your Canvas
     
     void OnMouseDown()
     {
         Debug.Log("Pressed left click.");
         myCG.interactable = true;
         myCG.blocksRaycasts = true;
         myCG.alpha = 1;
     }
 }

But make sure the canvas is active as the CanvasGroup will take care of making it visible and interactable.

Always get the = true and (true) wrong! Sorry @Flowered.


canvasnotactive.png (5.3 kB)
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 Flowered · Feb 10, 2015 at 08:33 PM 0
Share

Hello $$anonymous$$mmpies,

Unity telling me this when trying your script;

Assets/$$anonymous$$enuPopup.cs(22,24): error CS1955: The member `UnityEngine.CanvasGroup.interactable' cannot be used as method or delegate

Why does Unity do this? I'm depressed. Thanks for your time by the way :-)

avatar image Mmmpies · Feb 10, 2015 at 08:45 PM 0
Share

Just out will test when I get back maybe 3/4 of an hour.

avatar image Flowered · Feb 11, 2015 at 07:07 PM 0
Share

I have to thank you very much. Thank you for your time. &' btw, don't type on your phone again! - smiles :-))))

avatar image Mmmpies · Feb 11, 2015 at 07:08 PM 0
Share

Good advice @Flowered! Good advice :¬)

avatar image Flowered · Feb 23, 2015 at 10:35 PM 0
Share

When I want to remove all Buildings off my Hierarchy and get the CanvasGroup into my Prefab (which I want to instantiate), it is not possible to drag the Canvas into the inspector and attach it to the script.

It is only working when I have objects in my Hierarchy and do all the stuff there. I cannot use it with prefabs. Do you have any idea ?

Show more comments
avatar image
1

Answer by shriya · Feb 09, 2015 at 06:00 AM

Hi, I have attached the script.Initially your button should be inactive in hierarchy. Now on clicking mouse it will get active on screen. `using

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class asdf : MonoBehaviour {
 
     public GameObject abc;//Its your button
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
     
         if(Input.GetMouseButtonDown(0))
         {
             Debug.Log("Pressed left click.");
             abc.SetActive(true);
         }
     }
 }
 

Hope this will help you. You can place the code in start as per your need.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Displaying public variables in the inspector 2 Answers

Hide Object in Editor Only 5 Answers

on collision, show/hide other model scripting ? 1 Answer

Reloading a Scene throws NullReferenceException on GO with nested Prefabs 0 Answers

How to hide AdMob ads? 1 Answer


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