Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JamesClider · May 08, 2015 at 08:19 PM · classenum

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
     }
 }








Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JamesClider · May 09, 2015 at 06:09 AM 0
Share

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

avatar image hbalint1 · May 09, 2015 at 07:18 AM 0
Share

I updated the answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges