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 Rambit · Oct 04, 2013 at 06:24 PM · guifunctiononguiassigntooltip

Variable won't change, any suggestions?

Hi!

I am using a custom tooltip function (credits to AngryAnt). But I have a problem when trying to make it possible to have multiple tooltips. In the post as far as I understand, "tooltip" is the string to be replaced by the tooltip text, so I changed it to:

 void StringToolTip (Rect rect, string toReplace, string lastToolTip)
     {
         if (rect.Contains (Event.current.mousePosition))
         {
             toReplace = lastToolTip;
         }
     }
 
 void TooltipLastLayout(string toReplace, string lastToolTip)
 {
     StringToolTip (GUILayoutUtility.GetLastRect (), toReplace, lastToolTip);
 }

And call it like this in OnGUI:

 TooltipLastLayout (stringToDisplay, "This is a tooltip");

Where "stringToDisplay" is used by the label where the tooltip are to be displayed.

The problem is that "toReplace" won't change to "lastToolTip". I think it has something to do with the fact that you can't assign a value that is "layered" like "toReplace" (?) Does anyone know why this is happening?

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
Best Answer

Answer by jacksmash2012 · Oct 04, 2013 at 06:48 PM

toReplace is a local variable within the StringToolTip function. You can assign in whatever you want within that function, but that toReplace is a completely different memory location that the toReplace variable in your ToolTipLastLayout function, even though you have named them the same thing.

What I would suggest is that you change the signature of StringToolTip to this:

string StringToolTip (...) {...}

and then you can return the updated value. So your code could be something like:

 string StringToolTip (Rect rect, string toReplace, string lastToolTip)
     {
        if (rect.Contains (Event.current.mousePosition))
        {
          return lastToolTip;
        }
 
        return toReplace;
     }
  
 string TooltipLastLayout(string toReplace, string lastToolTip)
 {
     toReplace = StringToolTip (GUILayoutUtility.GetLastRect (), toReplace, lastToolTip);
     return toReplace;
 }
Comment
Add comment · Show 3 · 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 dunawayc · Oct 04, 2013 at 08:13 PM 1
Share

Your StringToolTip method has a return type of void, so you cannot return anything from it. You need to make the return type string.

avatar image Rambit · Oct 04, 2013 at 09:21 PM 0
Share

Thanks for the quick reply!

I tried your code, but still the same results (I changed the return type from void to string)

Any ideas?

avatar image jacksmash2012 · Oct 04, 2013 at 09:44 PM 0
Share

stupid me... sorry for the confusion with the return value. i'll correct it.

avatar image
1

Answer by whydoidoit · Oct 04, 2013 at 09:34 PM

You need to put a ref in front of the string parameters that are to change - then you can change the value that is being passed in. Or you could return strings from both of the functions.

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 Rambit · Oct 04, 2013 at 09:49 PM 0
Share

Wow, thanks! I put ref infront of every reference (?) and changed "This is a tooltip" to a variable, and now it works perfectly.

Just one question, what is the ref keyword(I think I went a little overboard with them)? Do you have any links or explanation?

Final code:

 string StringToolTip (Rect rect, ref string oldString, ref string lastToolTip)
     {
         if (rect.Contains (Event.current.mousePosition))
         {
             return lastToolTip;
         }
         return oldString;
     }

 void TooltipLastLayout(ref string toReplace, ref string lastToolTip)
 {
     toReplace = StringToolTip (GUILayoutUtility.GetLastRect (), ref toReplace, ref lastToolTip);
 }

and calling it like this:

 TooltipLastLayout (ref stringToDisplay, ref tooltipText);
avatar image jacksmash2012 · Oct 04, 2013 at 09:53 PM 1
Share

ref simply implies that you are passing in the memory location of the actual parameter, and so the string that is referenced by "oldString" is being updated, whereas previously you were assigning "oldString" to a new string completely.

avatar image whydoidoit · Oct 04, 2013 at 09:53 PM 1
Share

So a string is an immutable reference, changing it inside the function just changes the local copy. If you want to update the passed in reference then that's what ref is for. You only need it on the first parameter.

avatar image jacksmash2012 · Oct 04, 2013 at 09:54 PM 0
Share

yeah, his explanation was better :)

avatar image Rambit · Oct 04, 2013 at 10:01 PM 0
Share

Ahhh, that's very handy. Both of you; thanks for the help! ;)

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

18 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

Related Questions

OnGUI order, function executed before OnGUIs 0 Answers

GUILayout ArgumentException when combining with GUI.Tooltip 1 Answer

How completly remove GUI.Repaint() calls ? 1 Answer

Why can't I get my tooltip to show only when there is a tooltip set? 2 Answers

UI system multiple ints as function arguments 4 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