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

Trouble with Simple Turn Order Script C#

Discussion in 'Scripting' started by 80ProofGaming, Jan 6, 2015.

  1. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    I've been trying to make some simple code to handle my turn order for me. It seemed like it was going to work fine until I tried to incorporate a button on the GUI as a manual way to end the turn. I can't figure out how to use the system I have and use a button on screen to end the turn.

    Here's the code in my manager script:
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    public class Manager : MonoBehaviour {
    public int TurnStatus =0;

    void Update () {
    //Lets see if we can make some code to handle turn order
    if (TurnStatus == 0) {
    playerTurn ();
    }
    else if (TurnStatus == 1){
    enemyTurn();
    }
    }
    void playerTurn(){
    Debug.Log ("Player's turn");
    TurnStatus = 1; //make an if statement that asks if movement = 0 and AP = 0 or if the "end turn" button was pressed to change turn status to 2
    }

    void enemyTurn(){
    Debug.Log ("Enemy's turn");
    TurnStatus = 0; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
    }
    }
     
  2. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    By the looks of it 0 means it's your turn & 1 means it's the enemies turn, Why do you not just put a GUI button that sets the turnstatus to 1? making it the enemies turn. I don't really like your system though, How are you going to incorporate attacks? or movements?
     
  3. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    When I tried to do exactly that, I made a second script to plug into the button's event system that called the variable and changed it. I ran into a problem. I couldn't seem to figure out the correct way to call the variable from a different script. I am, admitedly, a novice. I only started attempting to learn C# recently. I had planned on incorporating the attacks directly into the playerTurn and enemyTurn methods so that the code would allow the player to make movements, then allow them to make attacks, then change TurnStatus. Would that work? If there's a better way, please let me know. I made this whole thing up in my head. I didn't use any example code or anything.
     
  4. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    Nice! you've done well so far.
    It should work if you put this in the script that controls the attacks.,
    Code (CSharp):
    1. Public Manager turnManager;
    then assign it in inspector
    or use this if it's attached to the same GameObject
    Code (CSharp):
    1. turnManager = gameObject.GetComponent<Manager>();
    then use
    Code (CSharp):
    1. turnManager.turnstatus = 1;
    once you finish attacking the enemy.

    if not make it static & you can access it by just using Manager.turnstatus .
     
    80ProofGaming likes this.
  5. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    Interesting, so the name of the script that the variable is in becomes part of the process of calling that variable. scriptname.variablename. There's always something new to learn it seems. I'll fiddle around with this today and see if I can get the end turn button working how I want.
     
  6. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    Trying to figure out how all this works, I looked up some information on the event system inside Unity. I tried to get this function to show up in the inspector:
    Code (CSharp):
    1.     public void TurnChange () {
    2.         if (TurnStatus == 0) {
    3.             TurnStatus = 1;  
    4.         }
    5.         else if (TurnStatus == 1){
    6.             TurnStatus = 0;  
    7.         }
    8.     }
    From what I understand, it has to be public, void, and take one or none of a small list of parameters. All those things seem to be true, so why isn't it showing up in the inspector as a function I can use? here's the rest of TurnManager as it exists right now.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TurnManager : MonoBehaviour {
    5.     public int TurnStatus =0;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     void Update () {
    13.         //Lets see if we can make some code to handle turn order
    14.         if (TurnStatus == 0) {
    15.             playerTurn ();  
    16.         }
    17.         else if (TurnStatus == 1){
    18.             enemyTurn();  
    19.         }
    20.     }  
    21.  
    22.    
    23.     void playerTurn(){
    24.         Debug.Log ("Player's turn");
    25.         TurnStatus = 1; //make an if statement that asks if movement = 0 and AP = 0 or if the "end turn" button was pressed to change turn status to 2
    26.     }
    27.    
    28.     void enemyTurn(){
    29.         Debug.Log ("Enemy's turn");
    30.         TurnStatus = 0; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
    31.     }
    32.  
    33.     public void TurnChange () {
    34.         if (TurnStatus == 0) {
    35.             TurnStatus = 1;  
    36.         }
    37.         else if (TurnStatus == 1){
    38.             TurnStatus = 0;  
    39.         }
    40.     }
    41. }
    42.