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 CopperCable · Jan 03, 2016 at 05:57 PM · c#uibuttoncanvasgroup

Disable and enable canvas group by pressing an UI button. (C#)

Hello everyone. I'm new to Unity and don't know how to script (C#) properly.

What i want to do id to show a canvas group by pressing one single button. I have a UI button set up that should enable a canvas group (Alpha, Interactable, Blocks Raycast) if pressed once and to hide again if pressed another time. Is that possible in Unity?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DarthHawk13 · Dec 28, 2016 at 05:29 AM

I created a tutorial video that answers your question. The button doesn't toggle however. I'll need to update the tutorial. :)

https://www.youtube.com/watch?v=iwxWrx98RfI

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 CopperCable · Jan 03, 2017 at 01:47 AM 0
Share

Thx for the tutorial, very helpful :)

avatar image
-2

Answer by jprocha101 · Jan 03, 2016 at 08:40 PM

You will need to:

  1. Create a script (see code below)

  2. Attach to a gameobject in your scene

  3. Assign the script to the OnClick event listener of your button

  4. Select the method from the script for the listener

The code to toggle whether canvas group is displayed or not would look similar to this:

 // This method will be set in the inspector in the OnClick section of your button
 public void ToggleCanvasGroupActive()
 {
    // This will set the canvas group to active if it is inactive OR set it to inactive if it is active
    MyCanvasGroup.gameObject.SetActive(!MyCanvasGroup.gameObject.activeSelf);
 }

I suggest you watch the official Unity tutorials because this is all covered there in detail. There are tutorials on scripting as well as how to handle the UI elements.

See this video for a detailed description of the UI button functionality.

See this video on activating gameobjects.

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 CopperCable · Jan 05, 2016 at 11:23 AM 0
Share

Thank you! I'll definitely check out those tutorials!

avatar image amanpu · Jan 05, 2016 at 11:52 AM 0
Share

Downvoted because question is about enabling/disabling Canvas Group , your answer is about disabling a canvas itself.

avatar image jprocha101 amanpu · Jan 05, 2016 at 01:38 PM 0
Share

I changed it say canvas group. Just switched out the variable name, same code.

avatar image Bonfire-Boy · Jan 05, 2016 at 12:03 PM 0
Share

Also, what is $$anonymous$$yCanvasGroup here? (or $$anonymous$$yGroupCanvas, presumably a typo for the same thing)

I think line 5 should just be

gameObject.SetActive(!gameObject.activeSelf);

And shouldn't step 3 say "assign the ToggleCanvasActive method in the new component to the OnClick event listener...", as per the comment in the code? (You don't assign a "script" to an event).

amanpu's issue regarding Canvas vs CanvasGroup is valid but now fixed. Ultimately this isn't a bad thing in the context of the question, since there's nothing special about Canvases or CanvasGroups in this context - using a button to toggle a GameObject's active status works the same way regardless of what components are on that GameObject.

avatar image Zogg · Sep 02, 2018 at 04:43 PM 0
Share

As @amanpu said, the question is about enabling / disabling the CanvasGroup component, not about activating / deactivating the CanvasGroup's game object. Unfortunately, CanvasGroup doesn't seem to have a CanvasGroup.enabled flag, unlike most components.

avatar image Bonfire-Boy Zogg · Sep 02, 2018 at 09:50 PM 0
Share

It was never clear to me what the OP was after. Yes the title mentions enabling and disabling a canvas group, but in the question they're also talking about "hiding" it. And if you look at the tutorial linked to in the other answer (which the OP said was helpful). it's just about making canvas groups invisible/non-interactable by changing their values. So I suspect the question is not about enabling/disabling in the sense you mean at all.

Anyway, what exactly is it you're trying to achieve? If you want to turn a canvas group component off, so that it's not having any effect on the ui elements that it's controlling, can't you just set the canvas group's fields accordingly? e.g set its alpha to 1 and so on.

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

9 People are following this question.

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

Related Questions

Hidden Canvas, doesn't disable interaction with buttons. 1 Answer

How do you access the Blocking Mask of GraphicsRaycaster in code? 2 Answers

Changing anchor positions on UI? 0 Answers

2D Collider Isnt working 1 Answer

How to display UI Text on GameObject in script? 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