Search Unity

Need help with turn-based

Discussion in 'Scripting' started by mister_thuesday, Nov 25, 2014.

  1. mister_thuesday

    mister_thuesday

    Joined:
    Nov 12, 2014
    Posts:
    3
    Helle everyone!

    I have GameController scrit wich sort all objects by initiative and change color active object.

    How i can make script wich will wait for pressing a button before the next object to paint

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class GameController : MonoBehaviour
    {
    public bool active;

    public List<Stats> lst = new List<Stats>(8);

    public int SelIdx; // selected index
    void Start()
    {
    active = true;
    Stats[] stats = FindObjectsOfType(typeof(Stats)) as Stats[];
    foreach (Stats stat in stats)
    {
    lst.Add(stat);
    }
    lst.Sort(delegate(Stats x, Stats y)
    {
    if (x.Initiative == y.Initiative) return 0;
    else if (x.Initiative < y.Initiative) return 1;
    else return -1;

    });

    SelIdx = 0;
    lst[SelIdx].transform.renderer.material.color = Color.red;


    StartCoroutine ("WaitAndChangeColor");


    }

    IEnumerator WaitAndChangeColor()
    {
    while (active)
    {
    yield return new WaitForSeconds(0.5f);
    lst[SelIdx].transform.renderer.material.color = Color.white;
    SelIdx = (SelIdx + 1) % lst.Count;
    lst[SelIdx].transform.renderer.material.color = Color.red;

    }
    }
    }
     
  2. federicopintaluba

    federicopintaluba

    Joined:
    Oct 19, 2014
    Posts:
    13
    Code (CSharp):
    1. buttonPressed = false;
    2.  
    3. while(buttonPressed){
    4.      // do something
    5.      buttonPressed = false;
    6. }
    buttonPressed is true when you press that button you want.

    The while(buttonPressed) MUST be in a coroutine.