- Home /
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
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 !
Answer by HarshadK · Feb 26, 2015 at 11:29 AM
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
}
Answer by kbaloch · Feb 26, 2015 at 11:53 AM
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
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Mouselook C#, which if/else statement? 1 Answer
My if statement won't work 1 Answer
C# if-statement optimization 1 Answer