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.
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.
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.
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.
var Hints : UI.Text;
Then in the inspector I would drag the item to that var, then control it like so.
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?
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?
@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.
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.
function OnMouseEnter () {
Hints.text = "Whatever you want it to say";
}
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
function OnMouseExit () {
Hints.text = "Whatever it originally said";
}
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.
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:
public class HintBinding : MonoBehaviour
{
public Text _hintDisplay;
public string _hintText;
public void UpdateHint()
{
_hintDisplay.text = _hintText;
}
}
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: _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.
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.