unity 2018.3.12f1 Button OnClick call function on another instance? of script
Hi, I am trying to create an end turn button. When it is clicked then it should change a value in Character script of an object that was moving. I have prefab of GameManager, this prefab has a script called TurnManager. TurnManager has function public void EndTurn() that I want to call after the button is pressed.
I've attached my prefab (from assets folder) to onClick()
Everything is ok when GameManager is on the scene and before play and I attach it to onClick, but when I instantiate it (from assets folder) through script after hitting play then I'm getting ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
here is my EndTurn and Start code (both on TurnManager script):
private List<Character> queue;
private List<Character> nextTurnQueue;
private GameManager gameManager;
private Transform[][,] map;
private IComparer<Character> initiativeComparer;
public void EndTurn()
{
StringBuilder b = new StringBuilder();
foreach (var c in queue)
{
b.Append(c.gameObject.name).Append(", ");
}
Debug.Log(b.ToString());
queue[0].SetTurnEnded(true);
}
void Start()
{
queue = new List<Character>();
gameManager = GetComponent<GameManager>();
map = GetComponent<BoardManager>().map;
foreach(Transform transform in map)
{
if(transform != null)
{
queue.Add(transform.GetComponent<Character>());
}
}
initiativeComparer = new InitiativeComparer();
queue.Sort(initiativeComparer);
nextTurnQueue = new List<Character>(queue);
StringBuilder b = new StringBuilder();
foreach( var c in queue)
{
b.Append(c.gameObject.name).Append(", ");
}
Debug.Log(b.ToString());
}
Interesting thing is that in debug log from the end of Start function the queue is not empty, and debug log from EndTurn function shows that there are no elements in the queue. I have to add that I'm not modifying queue only in Start function.