Question by
bh03ig · Mar 18, 2018 at 10:36 AM ·
enumreturn value
Can't return Enum state to script
Hi,
I am building a multi-class game and I have a small enum FSM. I decided to split the FSM into another "Players" script, while I have another script named "UI". "UI" accesses this Players script.
I've never attempted to return an Enum type, so i've never experienced an error like this before. The error I get is "UI.cs | A constant value is expected | Line 52". I don't understand why it would need to be constant. If I place the Enum in the UI.cs file, the problem goes away. I don't understand why it is any different from another script file.
Perculiarly, I don't get the error with the following code block commented out in Players.cs:
*/void Start ()
{
PlayersTurn = TurnStates.Player_1_Turn;
}*/
Players.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Players : MonoBehaviour {
public static Players Script;
public enum TurnStates { Player_1_Turn, Player_2_Turn };
#region PLAYER FSM
TurnStates PlayersTurn;
#endregion
void Awake()
{
Script = this;
}
/*
// Use this for initialization
void Start ()
{
PlayersTurn = TurnStates.Player_1_Turn;
}*/
// Update is called once per frame
void Update () {
}
public TurnStates getPlayerTurnState()
{
return PlayersTurn;
}
public void setPlayerTurnState(TurnStates State)
{
PlayersTurn = State;
}
public TurnStates getPossibleTurnState_P1()
{
return TurnStates.Player_1_Turn;
}
public TurnStates getPossibleTurnState_P2()
{
return TurnStates.Player_2_Turn;
}
}
UI.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI : MonoBehaviour {
public static UI Script;
#region UI
// Used to Display Time Passed since Game session start
public Text txtTimePlayed;
string TimeText = "Session Time: ";
int Time_Integer; // Hold Time.time integer from float
// Used to Display the amount of convoys in accordance with the Player FSM
public Text txtYourConvoys;
string YourConvoyText = "Your Convoys: ";
// Used to Display the current Players Turn with various variables
public Text txtPlayerTurn;
string PlayerTurnString = "Current Turn: ";
string Player1_Turn_Text = "Player 1";
string Player2_Turn_Text = "Player 2";
#endregion
void Awake()
{
Script = this;
}
void Start ()
{
}
void Update ()
{
}
public void update_SessionTime()
{
Time_Integer = (int)Time.time;
txtTimePlayed.text = TimeText + Time_Integer.ToString();
}
public void Update_Convoys_And_PlayerTurn()
{
switch (Players.Script.getPlayerTurnState())
{
case Players.Script.getPossibleTurnState_P1():
txtYourConvoys.text = YourConvoyText + Player_1_Convoys.ToString();
txtPlayerTurn.text = PlayerTurnString + Player1_Turn_Text;
break;
case Players.Script.getPossibleTurnState_P2():
txtYourConvoys.text = YourConvoyText + Player_2_Convoys.ToString();
txtPlayerTurn.text = PlayerTurnString + Player2_Turn_Text;
break;
}
}
}
Comment