Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Enum = Enum

Discussion in 'Scripting' started by antonioperez80, Aug 30, 2020.

  1. antonioperez80

    antonioperez80

    Joined:
    Jun 8, 2017
    Posts:
    40
    Hello, I would like to know how to change the value of an enum with the value of another of
    // in first script "firstScript"
    Code (CSharp):
    1. public enum characters{
    2. warrior,
    3. wizard
    4. }
    5. public characters char;
    6.  
    7. // in another script      "secondScript"
    8. public enum targets{
    9. warrior,
    10. wizard
    11. }
    12. public targets targ;
    13.  
    Example:

    gameobject.getcomponent<secondScript>().targ = gameobject.getcomponent<firstScript>().char;

    Thank you!
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You could convert to string and compare or use the numeric value, but there really is no need. Just use the same enum for both of the classes. You could do something like this;
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public enum CharacterType
    4. {
    5.     Warrior,
    6.     Wizard
    7. }
    8.  
    9. public class CharacterClass : MonoBehaviour
    10. {
    11.     public CharacterType Character;
    12. }
    13.  
    14. public class TargetClass : MonoBehaviour
    15. {
    16.     public CharacterType Target;
    17. }
     
  3. antonioperez80

    antonioperez80

    Joined:
    Jun 8, 2017
    Posts:
    40
    Thank you! It work!
     
    Duffer123 likes this.