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 /
  • Help Room /
avatar image
0
Question by Valkarth · May 10, 2017 at 03:15 AM · uigameobjectmousecomponenttoggle

Enabling and Disabling a Canvas

Hi,

I have created a C# script which is currently attached to the "PlayerModel" which controls the camera, movement and is the audio listener. In my scene I have an object called "Canvas" which is the parent of buttons, input fields and labels. I need to be able to enable and disable the checkbox in the Canvas component in Canvas.

I have tried declaring a public GameObject called menu and in the inspector I have set it to the Canvas. Code below:

 public void Update () {

     //If the user presses the left mouse button
     if (Input.GetMouseButtonDown (0)) {
         //The direction called forward is the |forward| x dimension of a vector
         Vector3 directionforward = transform.TransformDirection (Vector3.forward);

         //If there is an object attached with a collider in front of the object sending out the raycast. This condition is true.
         if (Physics.Raycast (transform.position, directionforward)) {
             Debug.Log ("You hit something");
             Canvas canvas = canvas.GetComponent<Canvas>();
             canvas.enabled = !canvas.enabled;
             canvas.enabled = true;
         }
     }
 }

The Debug currently works but I can't seen to get the menu to appear. How can I edit this C# script to be able to make the canvas and all UI items as it's children appear and disappear on mouse click.

Thanks!

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 FreeDooomLC · May 16, 2017 at 09:38 AM 0
Share

well that last part canvas.enabled = true should be enough, i Just used it like that, I have it in my script declared and in inspector set up (I'm only using it for the end game to appear so i only needed that one line)

maybe try to have an if statement that checks if canvas.enable == true, then change it to false, and other way around if canvas.enable == false changes it to true

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Adventure-Forge-Studio · Oct 13, 2017 at 11:05 AM

@Valkarth I think .Enable is deprecated at this point. You should use .SetActive(false/true);

canvas.gameObject.SetActive(false/true);

Comment
Add comment · Show 2 · 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 BenHowick · Nov 19, 2017 at 09:07 PM 0
Share

thanks. this helped a lot. big help

avatar image ValakhP BenHowick · Nov 19, 2017 at 09:24 PM 3
Share

@BenHowick, doublecheck it yourself. I believe, that is not true - not using latest unity version. But on 2017.1 everything is fine.

".enabled" and "gameObject.activeSelf" are two different things, they can't just replace one by another. $$anonymous$$oreover, using ".enabled" for canvas is prefferable than just turning object on/off. You can read about it here: https://unity3d.com/ru/learn/tutorials/topics/best-practices/other-ui-optimization-techniques-and-tips

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

160 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

Related Questions

Select Objects ingame and show information 0 Answers

Making GameObject move in the direction of my mouse (3d) 0 Answers

Toggle UI Text With UI Toggle Button 1 Answer

Mouse Events with Render Texture 0 Answers

Error getting button component with c# 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