- Home /
Different actions with same script called on differend objects
Hey,
Im creating a simple Point & Click game. If the player clicks on an object, the "Clicked" function in the "Clicked" script of that object is executed. Now I want different things to happen when the player clicks on different objects. Is there a way to call the same function on different objects, but having different code being executed?
Thanks in advance
Cheers, David
can i create an abstract class in javascript? or can i write one in c# and then use it in js?
Answer by The-Oddler · Nov 16, 2012 at 01:49 PM
There are two ways I can think of how to do this:
1: Inherit from your click script For each different thing that needs to happen, make a new script, that inherits from your click script. In that script program a custom click method.
2: Reaction scripts Make a new interface "reaction". Then make a bunch of reaction scripts for each of the different things you need an object to do (each implements the reaction interface.) Then add new reaction components to your object, and in the start of you click script collect there (GetComponents<...>()). In the click script, when 'Clicked' is called, call all the "React" methods of your reactions.
Hope this helps. If you don't know inheritance or interfaces, you should probably google them, they are pretty basic and not that hard.
Answer by astorga · Nov 16, 2012 at 02:03 PM
You can use SendMessage to call a function in another GameObject (or even another script at the same gameObject)
Your answer
Follow this Question
Related Questions
Stop Script Completely. Doesn't Execute Another Line Of Code. 1 Answer
Moving Objects along a vertices 1 Answer
Run Script From Level to Level 1 Answer
Is a clone object still a (clone) if you change the name of it using script? 1 Answer
Why does one script not work on multiple gameobjects? 3 Answers