- Home /
Click on another object
Hello.. How can I script that when I click on GameObject somethings happens? I was searching around here and I found only a script, that must be on the GameObject that I want to click on. But I need that script on another object (EmptyGameObject). I found something with raycast, but I don't know how it works. My main camera cannot move, it has stable position. If you know solution of my problem, help me please, I really appreciate it.
Marshall
Answer by CostelloNicho · Sep 25, 2013 at 07:14 PM
Hey marshall,
I don't know of an efficient way to do this without having a script on the game object. ( OnMouseDown function ) But a good way to go about this is to is to have a script that listens for the click on itself then broadcasts (or just sends) an event message. Look into events in C# there is plenty of documentation on MSDN. This way you can 'subscribe' to these events and handle them accordingly.
You mean that when I click on GameObject, it send information to the main script that this GameObject was clicked?
That is an option, but it will add to the coupling of your components which is what I was trying to stray you from. I like to set up events that other scripts (like your 'main script') can subscribe too if they need to know when the event has been fired.
This will let multiple scripts listen for the event if they want, and act independently of other objects.
These my GameObject have a name.. Can I somehow create, when I click on this GameObject, it will sand this name to the main script, and there it will find the right one GameObject in the list of GameObjects seted up in inspector?