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...
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.
Where is rend defined?
What happens in EyeDestroyed()?
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)
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...
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;
}
@$$anonymous$$errick20 Did this help? Have you found what the problem was?
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?
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!