1

I'm new to Unity and using 4.6 version.

I have a prefab which is just a sprite. And I instantiate three of them.

I want to receive touch and mouse events from those instances.

So, I added EventTrigger component to them from c# script, and added ClickEvent.

And I also added IPointerClickHandler and implement of it.

But it never gets any event. What am I missing?

Is TriggerSystem only works with UI system? Because I did not add any UI Pannel or UICanvas or etc. ( instead I added an empty object and added TriggerSystem component to it )

If this is not a good way to do, please give me a direction to start.

Thanks in advance.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
SeniorLee
  • 805
  • 1
  • 12
  • 25
  • Thank you guys. It never worked with EventSystem for me. Maybe it only works on touch devices. So I added BoxCollider2D and used Input class. And it works anyway. Maybe I should check performance. – SeniorLee Feb 19 '15 at 06:53
  • You need 2D Collider attached to that 2D Object/Sprite. http://stackoverflow.com/a/37456107/7444831 – Liwei Dong Jan 20 '17 at 06:30

2 Answers2

0

For GUI system to work there should also be an EventSystem object in your scene.

You can do it the easy way too, from the editor add a collider2d to the the sprite.

Write this to an script attach it the the gameObject.

void OnMouseDown() {
    // do something
}

Unity also convert mouse event to touch events on touch systems so you may not need to worry about simple touch events.

Dinal24
  • 3,162
  • 2
  • 18
  • 32
0

Below is for Mobile devices.

If you want to do in game controls without UI you can use Input

This is Unity document.

It pointed TouchPhase and all of touch phases can find here.

Unity document has good example codes i think you should check it.

Barış Çırıka
  • 1,570
  • 1
  • 15
  • 24