I am trying to use component based design in Unity3d v5. If I have 2 separate C# scripts attached to enemy ships like below:
-
Script1.shipDead() - Remove game
object and show explosion animation -
Script2.showPoints() - Destroyed
ship shows points ex: +100 (later
this will fade away)
How can I perform showPoints() function after shipDead() function has been performed, as a callback?
I can’t put both actions in the same function, as some enemies will not have points. Some enemies will leave power ups/ small enemies/ bombs after being destroyed.
And I don’t want to duplicated the same code in 4 functions.
Like :
- function 1: destroy ship (nothing more)
- function 2: destroy ship + show points
- function 3: destroy ship + spawn small enemies
- function 4: destroy ship + show points + spawn bomb
(As you can see above, there are many different possibilities / combinations I can come up with)
I need to separate functions and isolate them to do individual tasks. As shipDead() will not do anything related to showPoints() or showPowerUp() functions.
My question is how can I perform functions sequentially? One after another, like a callback function?
Like this:
- run Script1.DestroyShip() first;
- after that run Script2.ShowPoints();
- and finally run Script3.SpawnBomb();
Please provide a solution that will not violate the following:
- Components needs to be reusable (I can drag and drop the scrip in to other enemies/ships)
- Code shouldn’t be duplicated
- Loosely coupled (Lesser dependancies)
Please help me on this, Thanks in advance!