Use Enum of another script in an if statement C#

Hi all,

Got a problem with an Enumerations and an if Statement.

I have my script #1 :

using UnityEngine;  
using System.Collections.Generic;

public class weaponProperties : MonoBehaviour {  

	[System.Serializable]
		public enum weaponType {
			Range,
			Melee
		}

		public weaponType Type;

}

And I have my if statement in Script #2 :

weaponProperties weaponPropertiesComponent = GetComponent<weaponProperties>(); //get the script#1

		if (weaponPropertiesComponent.Type == weaponPropertiesComponent.Type.Range) {
			//do something
		}

I got this error :

error CS0176: Static member `weaponProperties.weaponType.Range’ cannot be accessed with an instance reference, qualify it with a type name instead

I Just wanted to change things on my code when I check Range or Melee on my enumeration

41431-bdd.png

I dont understand how to get my enum Type on the if statement in the Script#2… Someone can help me please ?

Thanks in advance !

Your if statement from Script #2 should be:

if (weaponPropertiesComponent.Type == weaponPropertiesComponent.Type.Range) {
    //do something
}

should be:

if (weaponPropertiesComponent.Type == weaponProperties.weaponType.Range) {
    //do something
}

You can declare enum from outside the class declaration. This way it will accessible for whole assembly (C#). like

//file weaponProperties

 public enum weaponType {
             Range,
             Melee
         }
 
 public class weaponProperties : MonoBehaviour {  
 
     // your class code is here
 
 }

// in your script #2
weaponType type;    // accesses enum
if(type == type.Range) {
// your stuf code goes here
}