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
0
Question by Annig · May 27, 2015 at 08:35 AM · canvasvariablealphatogglevalue

Storing the alpha value of a canvas object to a variable

Hi all,

Puzzling on yet another challenge in this (for me) new language. What I'm trying to do is toggle the visibility of a Panel object (belonging to the Canvas). However, while other GameObjects I've encountered thusfar have a Mesh Renderer, this item has a Canvas Renderer -- which doesn't carry the rend.enabled/disabled function. Why, Unity, why.

So instead, I'm trying to use the alpha value (transparancy) to toggle the visibility. Making it appear the first time is no biggie. However, making it disappear again is problematic. After a first if-statement, in which I check whether the space bar is pressed and the Bouncing function is activated, I tried using the .GetAlpha function to test the alpha value against 1.0; this would check whether it was already visible, so then it could be turned invisible. So it'd be:

if(cv.GetAlpha=1.0f) { set alpha to 0.0}

This gives the following error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer. Which made me think that the alpha value must be stored in a variable, before I can check it against another value. However, when I tried storing it into a float, I get error CS0428: Cannot convert method group GetAlpha' to non-delegate type float'. Consider using parentheses to invoke the method. Now I can't really figure out which type of variable I should store it as, because I used SetAlpha to give it a float value earlier in the code, and that works just fine.

Below is the code I'm trying to debug. If anyone has a solution, I'd be very grateful! public class visibility_panelBP : MonoBehaviour {

     public CanvasRenderer cv;
     public Bouncing startBPBouncing;
     private float value_alpha;
 
     // Use this for initialization
     void Start () {
         startBPBouncing = GameObject.Find("Bloedplaatjes").GetComponent<Bouncing>();
         cv = GetComponent<CanvasRenderer> ();
         cv.SetAlpha(0.0f);
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown ("space") && startBPBouncing.enabled == true) {
             value_alpha = cv.GetAlpha;
             cv.SetAlpha(1.0f);
             if(cv.GetAlpha=1.0f){
                 cv.SetAlpha(0.0f);
             }
             else {
                 cv.SetAlpha(1.0f);
             }
             
         }
     
     }
 }
 
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

1 Reply

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

Answer by spiceboy9994 · May 27, 2015 at 05:18 PM

GetAlpha is a method... so you need to do

 cv.GetAlpha()

http://docs.unity3d.com/460/Documentation/ScriptReference/CanvasRenderer.GetAlpha.html

Regards

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 Annig · May 28, 2015 at 09:07 AM 0
Share

Wow. Thank you. Excuse me while I throw myself off a building for missing that.

avatar image Hellium · May 28, 2015 at 09:09 AM 1
Share

@Annig : Please, tag the question as resolved by validating spiceboy9994 's answer ! ;)

avatar image Annig · May 28, 2015 at 09:20 AM 0
Share

@Hellium: How do I do that, exactly? I was under the impression that when posting an answer as opposed to a comment, the question is consequently tagged as answered. Sorry, I'm really just starting to find my way around ^^;

avatar image Hellium · May 28, 2015 at 03:29 PM 0
Share

@Annig : Just under the down vote button of spiceboy9994's answer (on the left), there is a check mark. Just click on it ! ;)

avatar image Annig · May 30, 2015 at 02:10 PM 0
Share

@Hellium: Check ;) thanks!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

UI canvas, toggle question 0 Answers

How Can I save my values with Toggles in UI canvas 4.6 through scenes? 3 Answers

“Flatten” Canvas Group before fading alpha (hiding UI Image overlaps) 1 Answer

CanvasGroup.alpha hides the UI in Scene view but not in game view. 1 Answer

Can't update value of var in another class 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