Search Unity

Question About Enim

Discussion in 'Scripting' started by DkuCook, Aug 30, 2014.

  1. DkuCook

    DkuCook

    Joined:
    Aug 29, 2014
    Posts:
    10
    Hey Guys I am hoping someone with a bit more experience can answer a question I have been looking at all night. I am very new to programming in general so there is a chance that I have found my answer but been a bit to uneducated to realize it.

    I am making a side scrolling game were the environment constantly pans to the right. Once a section of the environment has panned completely off screen I have a trigger in place to disable the sprite and relocate it. Originally I create 3 bool variants:

    bool ZoneActive
    bool ZoneReady
    bool ZoneIdle


    Then by updated the true/false states of these variants, I was able to get the function working as I intended. I then found a post on Enim. From my perspective being able to collapse these 3 variants into a drop down list was a much more suitable method for storing these 3 different states.

    After a bit of searching I was able to get my script to a stage were dependent on the state of my enum I was able to access a different section of code. This code can be seen below:

    Code (CSharp):
    1.     // Update is called once per frame\\
    2.     void Update ()
    3.     {
    4.         switch(CurrentStateOfZone)
    5.         {
    6.             case ZoneState.Idle:
    7.                 Debug.Log ("Zone Idle");
    8.                 break;
    9.             case ZoneState.Ready:
    10.                 Debug.Log ("Zone Ready");
    11.                 break;
    12.             case ZoneState.Active:
    13.                 Debug.Log ("Zone Active");
    14.                 break;
    15.         }
    16.     }
    The code I am trying to use to change the enum value can be seen below:

    Code (CSharp):
    1.     void ZoneCullingFunction ()
    2.     {
    3.         if(ZoneEnd_A.transform.position.x < -10)
    4.         {
    5.             Debug.Log("Zone Deactivated");
    6.             CurrentStateOfZone = ZoneState.Idle;
    7.         }
    8.     }
    Is what I am trying to do possible? Or can enum only be changed before the game itself is running? I dont want to change to stored values within the enum just with stored value is currently being accessed.
    As I said I am very new to programming in general, so dumped down answers would be greatly appreciated. Even if that is just a simple no it down not work.
    Thanks Guys
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    What your doing doesn't ring any alarms to me. Enums are very useful, when you're more experienced events and delegates would be even better. but for now enums should be fine, it kinds of seems like you've answered your questions since you've already done it right?
     
  3. DkuCook

    DkuCook

    Joined:
    Aug 29, 2014
    Posts:
    10
    The problem I am having is that it the below code doesn't seem to be changing the current state of my enum. That is what I am trying to find out, how do your write the code to change the current state of the enum?

    Code (CSharp):
    1. CurrentStateOfZone = ZoneState.Idle;
    The code above doesn't seem to effect the variable at all. Which makes me think that I am using incorrect code to try and change my enum value. For example when I first tried to create the block of code below:

    Code (CSharp):
    1.         // Update is called once per frame\\
    2.         void Update ()
    3.         {
    4.             switch(CurrentStateOfZone)
    5.             {
    6.                 case ZoneState.Idle:
    7.                     Debug.Log ("Zone Idle");
    8.                     break;
    9.                 case ZoneState.Ready:
    10.                     Debug.Log ("Zone Ready");
    11.                     break;
    12.                 case ZoneState.Active:
    13.                     Debug.Log ("Zone Active");
    14.                     break;
    15.             }
    16.         }
    17.  
    I original tried to use if statements, which didn't work at all. It was only after a bit of searching that I found a video demonstrating using switch & case which made it work perfectly. So there a certain way you have to write the change within your script for it to work properly?

    My enum set up is posted below:

    Code (CSharp):
    1.     public enum ZoneState {Idle, Ready, Active};
    2.     public ZoneState CurrentStateOfZone;
     
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    It's not working because of your if statement, the scenario never happens. you're switching it the right way.

    Here's the test I did:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnumTest : MonoBehaviour {
    5.  
    6.     public enum ZoneState {Idle, Ready, Active};
    7.     public ZoneState CurrentStateOfZone;
    8.  
    9.     void Update () {
    10.         if (Input.GetKeyDown (KeyCode.Space))
    11.             CurrentStateOfZone = ZoneState.Ready;
    12.  
    13.         switch(CurrentStateOfZone) {
    14.         case ZoneState.Idle:
    15.             Debug.Log ("Zone Idle");
    16.             break;
    17.         case ZoneState.Ready:
    18.             Debug.Log ("Zone Ready");
    19.             break;
    20.         case ZoneState.Active:
    21.             Debug.Log ("Zone Active");
    22.             break;
    23.         }
    24.     }
    25. }
    26.  
    Works perfectly so: if(ZoneEnd_A.transform.position.x<-10) must not be happening
     
  5. DkuCook

    DkuCook

    Joined:
    Aug 29, 2014
    Posts:
    10
    Got it working now. You were right I hadn't included the the trigger condition in my update function, I have it set in a different void to keep things organized and never called it, Noob Move on my part tanks for your time & advice.