Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

New Problems With UI Text

Discussion in 'Scripting' started by Studio_Akiba, Mar 31, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    The reason I stated in the title saying this was new is because I have used the same line before, without a single problem.
    This void was to be linked to a button to change some text on hover.
    However I am getting a error complaining about the = after TextObject.text, this is exactly the same way I have used it before, and yes I do have "using UnityEngine.UI;" at the top.
    Code (CSharp):
    1. public void TextChange (Text TextObject, string Text);
    2.     {
    3.         TextObject.text = "" + Text;
    4.     }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    You have a ";"-character at the end of the first line. Remove that and everything should be OK.
     
  3. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    ahh, stupid mistake, first time using two variables with buttons, reflex I suppose.
    Thanks.
     
  4. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Update: TextChange is not in the Event Trigger list, the new UI system does support multiple variables like I have done, right?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No
     
  6. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    so, how would I use more than one variable then? The new UI system would become very limited to me if I am only able to use 1 at a time
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Wrap them in a data class.
     
  8. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    How would one do this in my case, did some research and if it is what I think it is, I cannot see how that would help me.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class UIManager : MonoBehaviour {
    6.    
    7.     public void ChangeScene (int sceneToChangeTo)
    8.     {
    9.         Application.LoadLevel (sceneToChangeTo);
    10.     }
    11.  
    12.     public void LoadWebsite (string Website)
    13.     {
    14.         Application.OpenURL (Website);
    15.     }
    16.  
    17.     public void DropDown(GameObject DropDown)
    18.     {
    19.         DropDown.SetActive(true);
    20.     }
    21.  
    22.     public void TextChange (Text TextObject, string Text)
    23.     {
    24.         TextObject.text = "" + Text;
    25.     }
    26. }
     
  9. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Anyone know how to get this working?
     
  10. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    You most likely want to create another MonoBehaviour that contains the Text object you are interested in changing. Because that new script will already know the object it wants to modify, it can take just a single parameter -- the text it wants to change.

    It's probably annoying to create a new script "just" for this purpose, but tying parameters to buttons is meant to be lightweight and simplistic -- something that you might want to change while the game is running. If you can't think of a good reason you'd need to change the Text object reference on the fly, then that reference should probably be specified like other GameObjects -- explicitly through the Editor onto a MonoBehaviour script.
     
  11. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Essentially, all we are trying to achieve is having the text inside our Hints box change when hovering over buttons (from a string) but also do things when we click on them, I thought this would be the way to do it, but apparently not, its a pretty big limitation though.
     
  12. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @Galf So, if I have a single MonoBehaviour script with this line in for example:
    Code (CSharp):
    1. public Text Hints;
    How would I then link that to the button script above? I cannot see any way of this working easily.
     
  13. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    I found that dealing with text was kinda annoying when I started with the newer unity's UI. But after playing with it I found that the best way for me to manipulate the text objects within the UI was to create a variable and attach the text to that.
    Code (JavaScript):
    1. var Hints : UI.Text;
    Then in the inspector I would drag the item to that var, then control it like so.

    Code (JavaScript):
    1. Hints.text = "Whatever you want it to say";
     
  14. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    This is exactly the way I have handled it in the past too, but my problem is changing it by hovering over a button.
    I mostly work in C#, but I started out with UnityScript and am quite good at converting it.

    Have you managed to achieve changing the UI text before with a button event?
     
  15. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    I'll try and work up an example for you, but can you give me a better description of what you're trying to do? Right now, I picture that you have a single "Hint" Text object on the screen, with multiple buttons. Each of these buttons has an associated hint, that when you hover over any of them, they change the hint text to their specific hint. And I guess they have some other functionality associated with clicking?
     
  16. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @Galf Yes, essentially we are trying to have a hints text component at the bottom of the screen to tell you what that menu button does and what you could do in it when you hover over that specific button for example:
    Hovering over Multiplayer would print "Play online against multiple players in a selection of Gamemodes".
    Hovering over Options would print "Change display audio and gameplay options such as resolution and controls".

    This is used well in most AAA games to better show new gamers what more abstract menus may do before they actually click on them.
     
  17. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Use the function OnMouseEnter and OnMouseExit. I've used that since I started unity. Just merge that with similar code to what I posted above for the mouse over effect change.
    Code (JavaScript):
    1. function OnMouseEnter () {
    2.     Hints.text = "Whatever you want it to say";
    3. }
    You wouldn't even need an OnMouseExit if you wanted the mouse over effect permanent. Otherwise if you want it to go back after you scroll out just change it to
    Code (JavaScript):
    1. function OnMouseExit () {
    2.     Hints.text = "Whatever it originally said";
    3. }

    EDITED: obviously you'd have to convert that over to C#
    void OnMouseEnter() {
    And so on for the variable if there are any changes in context.
     
    Last edited: Apr 3, 2015
  18. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    I think you might be trying to force your initial solution of keeping this functionality in your manager class. Quickly, here's the type of solution you'd probably want to do:

    Code (CSharp):
    1. public class HintBinding : MonoBehaviour
    2. {
    3.   public Text _hintDisplay;
    4.   public string _hintText;
    5.  
    6.   public void UpdateHint()
    7.   {
    8.      _hintDisplay.text = _hintText;
    9.   }
    10. }
    You'd then attach this script to a button that has hint data, give that same Button an EventTrigger for PointerEnter, and have that call this new UpdateHint function, with no parameters. Obviously make sure to assign the Text field to your Hint Display within the Editor.

    If you really wanted to use your existing manager functionality, just change the above so that HintBinding now has another field for your UIManager, and then replace the UpdateHint() body with:
    Code (CSharp):
    1. _manager.TextChange(_hintDisplay, _hintText)
    , and once again, you'll be able to assign that functionality in the Editor.

    I'd imagine that the one-parameter restriction is largely due to clean display issues, but in many cases, I think that limitation can encourage better coding practices. Here, any functionality should either live with the associated button, or with the hint display. The manager has no direct association to either piece, which means the manager will likely become a huge mess of code over time.
     
  19. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Agreed, I would lean towards a HintDisplay component of some sort that changes text on hover-over, rather than going through a UIManager. Just apply the component to objects that you want that functionality for.