- Home /
add onMouseDown event on runtime created Game Object
I create some game object on runtime... for example I have 3 GameObject: cube, plane, car. I want add onMouseDown Event cube and plane, not car... How I can it?
Answer by robertbu · Feb 19, 2014 at 10:17 PM
There are two approaches. The first is that you create a prefab that already has the script on it and use Instantiate() to create the object at runtime. More information on prefabs here:
https://docs.unity3d.com/Documentation/Manual/Prefabs.html
The second is you write the script and then you attache it at runtime. Say the script is called 'MouseHandler' and you want to attach it at runtime:
theGameObject.AddComponent(MouseHandler);
Your answer
Follow this Question
Related Questions
Change Mesh/Gameobject onmousedown?,Swap mesh/gameobject randomly onmousedown 0 Answers
How can I move a player toward a gameobject by clickling an image 0 Answers
Programmatically call OnMouseDown() of a GameObject [solved] 1 Answer
Conflicting Box Colliders 0 Answers
OnMouse...() Does not working. 0 Answers