- Home /
Having an Error with An Enum : error CS0176: Static member `BossOne.BossActionType.Attacking' cannot be accessed with an instance reference, qualify it with a type name instead
new to enum states, trying to get it to chage state when a player gets in range....
error CS0176: Static member `BossOne.BossActionType.Attacking' cannot be accessed with an instance reference, qualify it with a type name instead
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossOne : MonoBehaviour {
public enum BossActionType
{
Aleted,
Dorment,
Attacking
}
public BossActionType BossStance;
// Update is called once per frame
void Update () {
switch (BossStance)
{
case BossActionType.Dorment:
break;
case BossActionType.Aleted:
break;
case BossActionType.Attacking:
break;
}
}
void OnTriggerEnter (Collider other){
if (other.gameObject.tag == "Player") {
BossActionType BossStances = BossStance.Aleted;
}
}
void OnTriggerExit (Collider other){
if (other.gameObject.tag == "Player") {
BossActionType BossStances = BossStance.Dorment;
}
}
Switch to Java coding. No such stupid C# errors arise.
Answer by Cornelis-de-Jager · Jul 07, 2017 at 01:54 AM
Skip to third point for Solution. The rest explains other problems you are going to encounter.
Within C# you can have more than one variable with the same name, if the new variable is nested within a loop or a function. So the problem is with these lines:
void OnTriggerEnter (Collider other){
if (other.gameObject.tag == "Player") {
BossActionType BossStances = BossStance.Aleted;
}
}
void OnTriggerExit (Collider other){
if (other.gameObject.tag == "Player") {
BossActionType BossStances = BossStance.Dorment;
}
}
Each time a check is done you create a new variable called BossStances . And as soon as you leave the If function that checks for the tag that variable is lost. So simply write:
void OnTriggerEnter (Collider other){
if (other.gameObject.tag == "Player") {
BossStance = BossStance.Aleted;
}
}
void OnTriggerExit (Collider other){
if (other.gameObject.tag == "Player") {
BossStance = BossStance.Dorment;
}
}
Secondly not sure why you are creating a new variable BossStance( s ). These variables just get destroyed and never used again with each check.
Lastly, you never assign an Stance to your BossStance variable, so add the following:
void Start () {
BossStance = BossActionType.Dorment;
}
Hi, thanks for the Input I already tried BossStance = BossStance.Alert; and got the Same error.
This problem is less of a Unity problem, and more a problem of your understanding of the program$$anonymous$$g. No offense intended. @Cornelis-de-Jager is right about how you should be assigning the boss stance, but all in all your code doesn't make sense. Especially your update switch statement, and the fact it kind of seems like your intention is to have a separate script for each boss, this is also improper as you can have a single Boss class, and use a component based system to provide different behaviors.
@zak666 did you add the following code?:
void Start () {
BossStance = BossActionType.Dorment;
}
Answer by zak666 · Jul 07, 2017 at 10:02 PM
Hi, thanks for the Input. Missed the START function to set the start Stance of the Boss, I have Already tried BossStance = BossStance.Attacking; that was the first thing I tried and I got
error CS0176: Static member `BossOne.BossActionType.Attacking' cannot be accessed with an instance reference, qualify it with a type name instead
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossOne : MonoBehaviour {
public GameObject UfoGun1;
public GameObject UfoGun2;
public GameObject UfoGun3;
public GameObject gunStartPos;
public GameObject gunAttackPos1;
public GameObject gunAttackPos2;
public GameObject gunAttackPos3;
public Material GunClosed;
public Material GunOpen;
public Material GunToChange1;
public Material GunToChange2;
public Material GunToChange3;
public bool WeekpointActive;
public enum BossActionType
{
Aleted,
Dorment,
Attacking
}
public BossActionType BossStance;
// Use this for initialization
void Start () {
GunToChange1 = GunClosed;
GunToChange2 = GunClosed;
GunToChange3 = GunClosed;
BossStance = BossActionType.Dorment;
}
// Update is called once per frame
void Update () {
switch (BossStance)
{
case BossActionType.Dorment:
HandleDormentState ();
break;
case BossActionType.Aleted:
HandleAletedState ();
break;
case BossActionType.Attacking:
HandleAttackingState ();
break;
}
}
void HandleDormentState(){
UfoGun1.transform.position = gunStartPos.transform.position; // LERP
UfoGun2.transform.position = gunStartPos.transform.position; // LERP
UfoGun3.transform.position = gunStartPos.transform.position;
}
void HandleAletedState(){
UfoGun1.transform.position = gunAttackPos1.transform.position; // LERP
UfoGun2.transform.position = gunAttackPos2.transform.position; // LERP
UfoGun3.transform.position = gunAttackPos3.transform.position; // LERP
StartCoroutine(WaitforAttack());
}
void HandleAttackingState() {
StartCoroutine(AttackGun1());
}
IEnumerator WaitforAttack (){
yield return new WaitForSeconds (3.0f);
BossStance = BossStance.Attacking;
}
IEnumerator AttackGun1(){
GunToChange1 = GunOpen;
WeekpointActive = true;
yield return new WaitForSeconds (3.0f);
GunToChange1 = GunClosed;
WeekpointActive = false;
yield return new WaitForSeconds (1.0f);
StartCoroutine(AttackGun2());
}
IEnumerator AttackGun2(){
GunToChange2 = GunOpen;
WeekpointActive = true;
yield return new WaitForSeconds (3.0f);
GunToChange2 = GunClosed;
WeekpointActive = false;
yield return new WaitForSeconds (1.0f);
StartCoroutine(AttackGun3());
}
IEnumerator AttackGun3(){
GunToChange3 = GunOpen;
WeekpointActive = true;
yield return new WaitForSeconds (3.0f);
GunToChange3 = GunClosed;
WeekpointActive = false;
yield return new WaitForSeconds (1.0f);
StartCoroutine(AttackGun1());
}
void OnTriggerEnter (Collider other){
if (other.gameObject.tag == "Player") {
BossStance = BossStance.Aleted;
}
}
void OnTriggerExit (Collider other){
if (other.gameObject.tag == "Player") {
BossStance = BossStance.Dorment;
}
}
}
Nope you missed it again.
BossStance = BossStance.alerted;
That makes no sense.
like a previous comment stated its
bossStance = BossActionType.alert;
Your answer
Follow this Question
Related Questions
How to declare and use a enum variable in Javascript? 2 Answers
Using Enum With Different Scrits 2 Answers
enum choose Random :) 4 Answers
EnumStates 1 Answer
Compare 2 enum variables method? 1 Answer