- Home /
How to access Enum from another class from the same script
Hello, so currently I'm working on a math game that shows The player 3 equations in which he have to choose the smallest one, the current issue I have now is that I can't seem to access the enum from class EquationData from my ShowEquation() function. I'll give an example of how I did it (search 'This Part'), but it didn't work so I'll need a hand. Thanks in advance.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Game : MonoBehaviour {
public EquationData[] Equation = new EquationData[4];
public Text Equation1;
public Text Equation2;
public Text Equation3;
public bool Turn = true;
public bool ShowEquation = false;
public int Timer= 0;
public int Health = 100;
public int TurnTime = 50;
public bool PlayerDamaged = false;
public int EnemyHitCounter = 0;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Timer += 1;
if (Timer == TurnTime)
{
Turn = !Turn;
Timer = 0;
}
//Player's Turn
if (Turn)
{
TurnTime = 300;
ShowQuestion();
}
//Enemy's Turn
if (!Turn)
{
TurnTime = 120;
ShowEquation = false;
Equation1.text = null;
Equation2.text = null;
Equation3.text = null;
PlayerDamage();
}
}
void ShowQuestion()
{
if (!ShowEquation)
{
EquationData data = Equation[1];
data.FirstNum= Random.Range(1,20);
data.SecondNum= Random.Range(1,20);
//This Part
EquationData.Equationtype = Random(0,3);
if (EquationData.Equationtype == 0)
{data.Result = data.FirstNum + data.SecondNum;}
if (EquationData.Equationtype == 1)
{data.Result = data.FirstNum - data.SecondNum;}
if (EquationData.Equationtype == 2)
{data.Result = data.FirstNum * data.SecondNum;}
if (EquationData.Equationtype == 3)
{data.Result = data.FirstNum / data.SecondNum;}
//All the way till here
Equation1.text = data.FirstNum.ToString() + data.SecondNum.ToString() + data.Result.ToString();
EquationData data2 = Equation[2];
data2.FirstNum= Random.Range(1,20);
data2.SecondNum= Random.Range(1,20);
data2.Result = data2.FirstNum + data2.SecondNum;
Equation2.text = data2.FirstNum.ToString() + data2.SecondNum.ToString() + data2.Result.ToString();
EquationData data3 = Equation[0];
data3.FirstNum= Random.Range(1,20);
data3.SecondNum= Random.Range(1,20);
data3.Result = data3.FirstNum + data3.SecondNum;
Equation3.text = data3.FirstNum.ToString() + data3.SecondNum.ToString() + data3.Result.ToString();
ShowEquation = true;
}
}
public void PlayerDamage()
{
if(!PlayerDamaged)
{
EnemyHitCounter += 1;
if (EnemyHitCounter == 20)
{
Health -= 10;
EnemyHitCounter = 0;
PlayerDamaged = true;
}
}
}
}
[System.Serializable]
public class EquationData
{
public float FirstNum;
public float SecondNum;
public float Result;
public Equationtype Equation;
public enum Equationtype
{
Addition,
Subtraction,
Multiplication,
Division
}
}
Answer by hbalint1 · May 08, 2015 at 08:26 PM
EDIT: I read it again and i saw you use the wrong variable. The Equationtype sis clearly a type, so you can not give it a value with the Random.Range and you can not compare with "==". You should use the EquationData.Equation for this. I saw you has that variable. ALso I don't see where you have reference to the EquationData class. Either you have to instantiate it like private EquationData equationData = new EquationData();
or I see you have an array of EquationData named Equation. So maybe you want to use that.
To use these you should have a single instance of your class as I mentioned above, so:
private EquationData equationData = new EquationData();
equationData.Equation = (EquationData.Equationtype)Random(0,3);
And for calling try EquationType.Addition and so on like:
if (equationData.Equation == EquationData.Equationtype.Addition)
or
if (EquationData.Equation == (EquationData.Equationtype)0) // this is working too! but the above one is nicer
Or use you EquationData[4] Equation array, so use the lines above with index, like:
Equation[0] = (EquationData.Equationtype)Random(0,3);
Now its giving me an error for this line
EquationData.Equationtype = (EquationData.Equationtype)Random(0,3);
and this line
if (EquationData.Equationtype == EquationData.Equationtype.Addition)
saying expression denotes a type' where a variable' value' or method group' was expected
Your answer

Follow this Question
Related Questions
PropertyDrawer: Enum to select extended class 0 Answers
Changing class variables 2 Answers
How do I create a bunch of player abilities, with their own properties? 2 Answers
Construct class with enum parameter (javascript) 0 Answers
Need assistance with SubType in custom class (UnityScript or C#) 2 Answers