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 /
  • Help Room /
avatar image
0
Question by Merrick20 · Sep 09, 2016 at 06:10 PM · c#enum

Problem assigning enum value

Hi everyone,

This is probably stupidly easy, but I'm busting my brains with this.

I made this Enum

 public enum EyeState{Enabled, disabled, destroyed};

Then created the variable in a monobehaviour

 public class EyesDamageReciever : MonoBehaviour {
 
           public EyeState myState;


The idea is, that when the player deals damage to it, instead of simply destroy the object it changes the enum value.

 IEnumerator CheckLife()
     {
         rend.material.color = Color.red;
         
         yield return new WaitForSeconds (0.25f);
         
         if (currentHP <= 0) 
         {
             if (myState == EyeState.Enabled)
             {
                 myAI.EyeDestroyed(this.name);
                 //ChangeMyState(myState);
                 myState = EyeState.disabled;
             }
         }
         yield return new WaitForSeconds (0.25f);
         rend.material.color = Color.white;
         
         yield break;
     }


But nothing is happening. I tried debugging it and a warning saying "The requested item has been unloaded" I don't know if this has something to do with the problem. I googled the warning but every result says they have problem reading the variable. I'm having problems changing it.

Any solutions? Am I making a wrong use of enums?

Sorry for my horrible english...

Comment
Add comment · Show 2
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 TBruce · Sep 09, 2016 at 06:29 PM 0
Share

What requested item are you referring to?

You are giving partial information on two to three classes (EyesDamageReciever, myAI and the one with the IEnumerator if not apart of EyesDamageReciever).

You say nothing is happening. What are you expecting to happen.

  1. Where is rend defined?

  2. What happens in EyeDestroyed()?

  3. How are you calling CheckLife()? (This is a coroutine, the proper way to call coroutines is StartCoroutine(CheckLife()), if you do not do this the coroutine will not be called)

avatar image Merrick20 · Sep 09, 2016 at 08:19 PM 0
Share

This is the complete Script

 sing UnityEngine;
 using System.Collections;
 using System.Linq;
 using System.Collections.Generic;
 
 public enum EyeState{Enabled, disabled, destroyed};
 
 public class EyesDamageReciever : $$anonymous$$onoBehaviour {
 
     public LibraryGuardianAI myAI;
 
     public EyeState myState;
 
     public DamageType[] weakness;
     public DamageType[] immuneTo;
     
     public float hp;
     public float currentHP;
     private Renderer rend;
 
     // Use this for initialization
     void Start () {
 
         rend = GetComponent<Renderer>();
         currentHP = hp;    
         myAI = GameObject.Find("LibraryGuardian").GetComponent<LibraryGuardianAI>();
     }
     
 
     void OnTriggerEnter2D (Collider2D col)
     {    
         DamageSource damageGiver = col.GetComponent<DamageSource> ();
         
         if (damageGiver) {
             
             
             foreach (DamageType typeOfDamage in damageGiver.damageTypeDealt) {
                 
                 if (!immuneTo.Contains (typeOfDamage)) {
                     
                     if (weakness.Contains (typeOfDamage))
                     {
                         currentHP -= damageGiver.damageDealt *2;                    
                         
                     }else{
                         currentHP -= damageGiver.damageDealt;}
                     
                     StartCoroutine (CheckLife ());
                     
                 }
             }        
         }
     }
 
     IEnumerator CheckLife()
     {
         rend.material.color = Color.red;
         
         yield return new WaitForSeconds (0.25f);
         
         if (currentHP <= 0) 
         {
             if (myState == EyeState.Enabled)
             {
                 myAI.EyeDestroyed(this.name);
                 //Change$$anonymous$$yState(myState);
                 myState = EyeState.disabled;
             }
         }
         yield return new WaitForSeconds (0.25f);
         rend.material.color = Color.white;
         
         yield break;
     }
 }
 

EyeDestroyed() is called on another script and it's being called. No error stops Runtime and it compiles. The property myState just doesn't change...

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Sep 09, 2016 at 09:15 PM

You say myState does not change. Do you mean this block

 if (myState == EyeState.Enabled)
 {
     myAI.EyeDestroyed(this.name);
     //ChangeMyState(myState);
     myState = EyeState.disabled;
 }

If so you need to verify some things. Try adding some debug statements like so

 IEnumerator CheckLife()
 {
     rend.material.color = Color.red;
 
     yield return new WaitForSeconds (0.25f);
 
     Debug.Log("CheckLife: currentHP = " + currentHP + ", myState = " + myState.ToString());
     if (currentHP <= 0) 
     {
         Debug.Log("currentHP is <= 0");
         if (myState == EyeState.Enabled)
         {
             // if you see this debug statement then you need to place some debug statements inside myAI.EyeDestroyed() - follow the trail till you find the culprit
             Debug.Log("myState == EyeState.Enabled, calling myAI.EyeDestroyed(" + this.name + ")");
             myAI.EyeDestroyed(this.name);
             //ChangeMyState(myState);
             myState = EyeState.disabled;
         }
     }
     yield return new WaitForSeconds (0.25f);
     rend.material.color = Color.white;
 
     yield break;
 }
Comment
Add comment · Show 4 · 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 TBruce · Sep 10, 2016 at 05:03 PM 0
Share

@$$anonymous$$errick20 Did this help? Have you found what the problem was?

avatar image Merrick20 · Sep 12, 2016 at 05:52 PM 0
Share

I took your advice. And did this:

 if (myState == EyeState.Enabled)
              {
                  Debug.Log (myState.ToString());
                  myAI.EyeDestroyed(this.name);
                  //Change$$anonymous$$yState(myState);
                  myState = EyeState.disabled;
                  Debug.Log (myState.ToString());
              }
          }
 

The script is working perfectly fine. It's the Inspector what is not working. Never happened to me this before. Is it some kind of bug? Anyone?

avatar image TBruce Merrick20 · Sep 12, 2016 at 06:15 PM 0
Share

And what happens when you remove the debug statements?

Since this answer helped you, would you please be so kind as to the "Accept" button above to accept the answer? Thank you!

avatar image Merrick20 · Sep 12, 2016 at 08:33 PM 0
Share

Ok. Altough it doesn't solve the Inspector problem...

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

232 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 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 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 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

C# missingReferenceException when comparing enums 0 Answers

Unity3d select random enum in another script 1 Answer

My public enum isn't functioning like I want it to 0 Answers

Enum comparison C# 1 Answer

Can I set one enum value equal to another enum value? 2 Answers


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