Search Unity

Using enums as a function parameter

Discussion in 'Scripting' started by GoodNight9, May 2, 2016.

  1. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Hello everyone! I have a question about using an enum from one script as a parameter in a function in a nother script:

    Here is my code for the script with the enum
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class scr_cutscene_camera : MonoBehaviour
    6. {
    7.     public enum CamZoom{none, zoom_in, zoom_out};
    8.     public CamZoom myZoom;
    9.    
    10.     void Start()
    11.     {
    12.         myZoom = CamZoom.zoom_in;
    13.     }
    14. }
    15.  
    And here is the script I'd like to control it with in some functions
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class scr_cutscene_controller : MonoBehaviour
    6. {
    7.     public GameObject camera_ref;
    8.     scr_cutscene_camera srcc;
    9.  
    10.     void Start ()
    11.     {
    12.         srcc = camera_ref.GetComponent<scr_cutscene_camera>();
    13.     }
    14.    
    15.     void Update ()
    16.     {
    17.         if (Input.GetKey(KeyCode.H))
    18.         {
    19.             srcc.myZoom = scr_cutscene_camera.CamZoom.zoom_in;
    20.         }
    21.         else
    22.         {
    23.             srcc.myZoom = scr_cutscene_camera.CamZoom.none;
    24.         }
    25.     }
    26.  
    27.     void CreateShot(enum CamZoom_variable)
    28.     {
    29.         srcc.myZoom = scr_cutscene_camera.CamZoom.CamZoom_variable;
    30.     }
    31. }
    32.  
    The problem is using a enum variable in the function. It isn't working. I understand that enums aren't really variables, and are kind of special, but I am not sure how to go about working around this. Thank you for your time!
     
  2. Kazen

    Kazen

    Joined:
    Feb 17, 2015
    Posts:
    68
    The enum variable sent to the function should be defined as your CamZoom enum.
    So your function should look like this:

    Code (CSharp):
    1. void CreateShot(CamZoom CamZoom_variable)
    but now that I think about it, your enum is inside your camera-scope, so you might even need to write:

    Code (CSharp):
    1. void CreateShot(scr_cutscene_camera.CamZoom CamZoom_variable)
    If you don't want to have it scoped to your camera cutscene class, move it outside the camera cutscene scope, or just move it to a separate, empty script and paste in your enum there.
     
    GoodNight9 likes this.
  3. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Thank you so much! That second option worked perfectly!

    Really appreciate the help Bro :)