Touchscreen Start Button

Hello,

I have a menu for my 2d android game now, and I have added a UI button to it.
Now on this button, i have this simple script attached on it:

void OnMouseEnter()
{
	Application.LoadLevel("Krolsch");
}

I can touch the button, but nothing happens, it doesn’t load the new scene.

I have heard that you can use a OnMouseEnter or OnMouseDown, and that should work as a touch?

Bram

Like said above just use the OnClick event in the inspector. Here’s what I do for UI.

  1. Make a new script for managing button clicks.

  2. Attach this script to some Gameobject in your scene.

  3. In the script, make a new public function for what you want your button to do…for example

  4. public void playButton() { Application.LoadLevel("Krolsch"); }.

  5. Now click on your button in the hierarchy and find the little area in the inspector that says "On Click () "

  6. Click the little plus symbol

  7. 56190-jdh.png

  8. This will add somthing with 2 drop down menus and an input thing that says “None (Object)”. Drag the game object that has your button script on it into that field that says “None (Object)”.

  9. This will let you reference this object. Click on the drop-down menu that says “No Function” and it will show some options. Click on the one that is the same name as the script you made earlier and then click on the function name. This will make the button run this function.

  10. For example, my script is called ButtonManager and my function was called playButton

By “UI” you mean uGUI or the legacy GUI?

In uGUI, for a UI element to react to touch events you can do various things:

  1. Use the Button “OnClick” event in the inspector, setup the function that should be called. Or…
  2. Implement the IPointerDownHandler interface in a script added to the button. This forces you to implement the OnPointerDown function.
  3. Use an EventTrigger component instead of the OnClick event of the button, and add and setup the PointerDown event in the inspector.

I’m not sure if OnMouseEnter should work for the UI, but if it should, I guess OnMouseDown would make a lot more sense.

Also, UI elements need an EventSystem game object in the scene to react to events. Make sure it’s not missing in your scene.