How would I set up an order for turns using multiple scripts?

In my game I have characters that are shot from a catapult at targets. They then walk back to the catapult to be shot again. (There will be about 7 or 8 characters total.) I am trying to figure out how I to set up an ordering system so they are shot only when it is their turn. Since each character has their own script, I’m not quite sure how to go about this.

I am using the catapult script to keep track of the number of characters “OnDeck” but I’m not sure how to tell them to take turns. Right now when I launch the character in the catapult, all the characters launch at the same time. Any suggestions would be greatly appreciated.

This is actually a very similar (almost identical) problem to recycling objects when they’re destroyed. Especially on mobile platforms its common practice to have a pool of objects such a projectiles that just keep getting reused when they are needed. It significantly reduces the amount of memory needed compared to instantiating new ones.

But I digress. For your problem, you’ll want your catapult to keep track of all the characters in an array. Then when its time to fire, you want to keep an index of the current fire position starting at 0. The first guy is launched, then we move the index forward 1 to the second. Then that character is launched when the time comes. And then we repeat.

It could look something like:

private var projectiles : YourScript[];
//Whatever script is attached to your characters
private var launchIndex = 0;

function Start () {
     projectiles = FindObjectsOfType( YourScript );

function Fire () {

     if ( projectiles[launchIndex].isAvailable ) {
     //some check to make sure the character isn't walking back still.
          //Launch the projectile
          if (launchIndex == projectiles.Length ) 
               launchIndex = 0;

I’d put the shooting stuff into a Coroutine. Then create a manager-script, that knows all catapults. Inside of that you use a coroutine, to launche the catapult-coroutines in order.

It’s kinda hard to explain like that, but some pseudocode would look like this in C#:


List<Catapults> catapults;
IEnumerator MakeCatapultsShoot() {
  int i = 0;
  // this actually waits
  // until the catapult-coroutine is finished
  yield return StartCoroutine(catapult*.StartShooting());*

if(i > catapults.Count) i = 0;
public IEnumerator StartShooting() {
… do shooting stuff
You’d have to call StartCoroutine(MakeCatapultsShoot) whenever you want a catapult to shoot, and they’ll take turns then.

I don’t know what your game looks like but couldn’t you have a physical queue that uses colliders and triggers the way the loading system of an automatic gun works? So all the characters are trying to move toward a closed invisible door, constrained by invisible walls and capsule colliders on the characters in front of them, and every time the catapult resets, the door opens, lets one into the catapult if it is standing there, then closes behind it? The catapult script doesn’t need to know which character it’s firing and the character script doesn’t need to know how to do anything except “try to walk forward until I can’t anymore”.

I’ve made that kind of thing long ago with Quake 1’s triggers, I’m sure it would be easier with the physics in Unity…