Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by zak666 · Jul 07, 2017 at 01:45 AM · enumstate

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;
 
         }
     }
 
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vollmondum · Jul 07, 2017 at 04:57 AM 0
Share

Switch to Java coding. No such stupid C# errors arise.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zak666 · Jul 07, 2017 at 02:44 AM 0
Share

Hi, thanks for the Input I already tried BossStance = BossStance.Alert; and got the Same error.

avatar image RobAnthem zak666 · Jul 07, 2017 at 02:51 AM 0
Share

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.

avatar image Cornelis-de-Jager zak666 · Jul 07, 2017 at 04:48 AM 0
Share

@zak666 did you add the following code?:

     void Start () {
          BossStance = BossActionType.Dorment;
      }


avatar image
0

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;
 
         }
     }
 
 
 
 }
 

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jwizard93 · Jul 07, 2017 at 11:37 PM 0
Share

Nope you missed it again.

 BossStance = BossStance.alerted;

That makes no sense.

like a previous comment stated its

 bossStance = BossActionType.alert;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges