Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Turn manager

Discussion in 'Scripting' started by mathieu548, Apr 28, 2021.

  1. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    Hello, I'm creating a card game and I want my next turn button to switch in between players whom are instances of a prefab. Let's call them "me" and "you" and "him" etc. up to 5 players. I wrote this code but can't find a condition where it stops and starts correctly.( I use a button for this)
    Code (CSharp):
    1.    
    2.     public Player active_player;
    3.     public List<Player> players; /* instantiated elsewhere you can do it yourself in a start if you want. Just don't forget the pseudo in Player script */
    4.  private static TurnManager instance;
    5.     public static TurnManager Instance => instance;
    6.     private void Awake()
    7.     {
    8.  
    9.         if (instance != null)
    10.         {
    11.             Destroy(gameObject);
    12.         }
    13.         else
    14.         {
    15.             instance = this;
    16.         }
    17.         players = GameManager.Instance.players; // where I instanciate it ignore if needed
    18.     }
    19.     public void NextTurn()
    20.     {
    21.         Player next_player;
    22.         Player current_player;
    23.         current_player = active_player;
    24.         for (int i = 0; i < players.Count; i++)
    25.         {
    26.             current_player.gameObject.SetActive(false);
    27.             Debug.Log(current_player.pseudo);
    28.             next_player = players[(i + 1) % players.Count];
    29.             Debug.Log(next_player.pseudo);
    30.             active_player = next_player;
    31.             Debug.Log(active_player.pseudo);
    32.             next_player.gameObject.SetActive(true);
    33.         }
    34.  
    35.     }
    36.     public void NextTurn()
    37.     {
    38.         TurnManager.Instance.NextTurn();
    39.     }
    40.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I'm afraid I don't quite understand what you mean by this.

    None of the above except Awake() is a Unity function, so I'm not sure when anything is called either.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you are running a mobile device you can also see the console output. Google for how.
     
    Joe-Censored likes this.
  3. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    I forgot this is in a button manager file separated
    Code (CSharp):
    1.    
    2. public void NextTurn()
    3.     {
    4.         TurnManager.Instance.NextTurn();
    5.     }
    6.  
    To answer your questions, it runs and shows "me" "you" "you" then "me" "me" "me" after clicking the "next " button
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I don't think you want the construct you have with the for() loop starting on line 24.

    I'm not even sure I can reason about what it does, but it iterates ALL players before it even exits.

    Instead, if you have N players (in a List<T> or Array), then keep track of which one you are on from 0 to N-1 with a simple integer.

    When it is time to change players, advance that integer by 1.

    When it reaches the end of the list, reset the integer to 0.

    In all cases, use that integer to look up the player in the list of players.

    That's all there is to round-robin turn-taking.
     
    mathieu548 likes this.
  5. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Just checking, does this code compile? You have two of the following in the same file:

    public void NextTurn()
     
  6. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    no they are in 2 separated files so yes it compiles
     
  7. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    Thanks a lot, should have known this! Now it works perfectly. I will be back later
     
    Kurt-Dekker likes this.