So I have some code that creates a list of GameObjects:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GoList : MonoBehaviour {
public GameObject[] enemyList;
public int enemyListLength;
public List<GameObject> GObj = new List<GameObject> ();
// Use this for initialization
void Awake () {
enemyList = GameObject.FindGameObjectsWithTag ("Character");
enemyListLength = enemyList.Length;
for (int i = 0; i < enemyListLength; i++){
GObj.Add (enemyList[i]);
}
GObj.Sort (SortMethod);
foreach (GameObject go in GObj) {
print (go.name);
}
GObj[0].GetComponent<PlayerTurn>().turnActive = true;
}
private int SortMethod (GameObject A, GameObject B){
if (!A && !B) return 0;
else if (!A) return -1;
else if (!B) return 1;
else if (A.GetComponent<Attributes>().speed > B.GetComponent<Attributes>().speed) return 1;
else return -1;
}
The GameObjects are ordered by a variable speed located on another script. I’m having trouble with coming up with a way to execute each characters turn. Every way that I attempted has either not worked, or wasn’t flexible enough to support varying numbers of characters. Right now I have a script on each character that has a bool turnActive set to false:
using UnityEngine;
using System.Collections;
public class PlayerTurn : MonoBehaviour {
public bool turnActive = false;
public GameObject battleBrain;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(turnActive) {
Turn();
}
}
public void Turn() {
Debug.Log ("taking turn");
turnActive = false;
}
}
When turnActive is true, the character executes his/her turn. The problem I’m having is telling the next character that they can now go, while being flexible enough to support different numbers of characters. A nudge in the right direction and maybe some help along the way would be great.