- Home /
Event Trigger trouble (4.6 UI)
I am trying to implement a system for an exp bar where the player hovers the mouse over the bar and it displays the current exp percentage. I am using the new ui system (4.6) and event triggers on a blank image that is placed over the actual exp bar. The setup on my trigger system is shown in the screen shot. The script implemented to display the exp percentage is as shown below:
public class Exp_Bar : MonoBehaviour
{
bool isHovering = false;
void OnGUI()
{
if (isHovering)
{
decimal exp = (decimal)Math.Round((active_player_profiler.plvl_exp / 1000f) * 100f, 2);
GUI.Box(new Rect(Input.mousePosition.x + 15f, Screen.height - Input.mousePosition.y, 70f, 20f), exp.ToString() + "%");
Debug.Log("yooooooo");
}
}
public void enableHover()
{
isHovering = true;
Debug.Log("enabled");
}
public void disableHover()
{
isHovering = false;
Debug.Log("disabled");
}
}
I placed the debug messages for testing purposes. When in game, nothing seems to work. I don't see the debug messages or the exp display. Also, I removed the EventSystem object as a test. I had it before on my canvas but it did not fix the issue. My canvas also has its default graphic raycaster. What seems to be the problem?
Answer by SneeKeeFahk · Jan 26, 2015 at 03:19 AM
I think you are missing an EventSystem object in your scene. Try adding one or add a new Canvas and that should add one for you.
Though if you were to read over the OP I did mention adding an eventsystem, the problem was I had the standalone input module disabled. For PCs and $$anonymous$$acs it needs to be enabled if you plan on using events that involve mouse input. I can disable the touch input module in this case because it is not needed. Thank you @Snee$$anonymous$$eeFahk :)
Answer by SnStarr · Jan 26, 2015 at 02:55 AM
In order to use the new UI system in Unity you need to add Using statements. Above the Class declaration of public class Exp_Bar : MonoBehaviour should be using UnityEngine.EventSystems and the script should also inherit from the IPointerClickHandler if using OnPointerClick events.
using UnityEngine;
using UnityEngine.EventSystems;
public class Exp_Bar : MonoBehaviour, IPointerCLickHandler
{
bool isHovering = false;
Even though this did not quite solve my issue I am voting up because I did not know this and also opened up some of my options for future code.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
What's your equivalent of old GUIStyle ? 0 Answers