- Home /
Why would my switch's case be correct but the output is not?
Not entirely sure what to ask so forgive the title.
Preamble
I have an ability on my player that highlights enemies for a brief time, all works well for this.
I decided to make this upgradable so the higher the level of the ability the more stuff gets highlighted.
Level 0 (base level) enemies are highlighted.
Level 1 enemies & Items are highlighted.
Level 2 enemies, items & special items are highlighted.
Level 3 enemies, items, special items & hidden areas are highlighted.
To do this I thought a switch running in an IEnumerator (Coroutine) would be the most efficient way. A function calls the coroutine and depending on the level the player has for the ability a switch should handle the the actual ability functionality (see code).
Here's the code.
public void CatsEye(){
if (player.cateye >= 1 && !catsEyeActive) {
player.cateye -= 1;
StartCoroutine("CatEye");
// Debug.Log("Cateye Active");
}
}
IEnumerator CatEye(){
catsEyeActive = true;
switch(player.cateyeLevel){
case 3:
if (itemObject != null && enemyObject != null
&& specialObject != null && hiddenObject != null) {
hidden.isOutlined = true;
special.isOutlined = true;
enemyData.isOutlined = true;
item.isOutlined = true;
}
yield return new WaitForSeconds(15f);
if (itemObject != null && enemyObject != null) {
hidden.isOutlined = false;
special.isOutlined = false;
enemyData.isOutlined = false;
item.isOutlined = false;
}
catsEyeActive = false;
break;
case 2:
if (itemObject != null && enemyObject != null && specialObject != null) {
special.isOutlined = true;
enemyData.isOutlined = true;
item.isOutlined = true;
}
yield return new WaitForSeconds(15f);
if (itemObject != null && enemyObject != null) {
special.isOutlined = false;
enemyData.isOutlined = false;
item.isOutlined = false;
}
catsEyeActive = false;
break;
case 1:
if (itemObject != null && enemyObject != null) {
enemyData.isOutlined = true;
item.isOutlined = true;
}
yield return new WaitForSeconds(15f);
if (itemObject != null && enemyObject != null) {
enemyData.isOutlined = false;
item.isOutlined = false;
}
catsEyeActive = false;
break;
case 0:
if (enemyObject != null) {
enemyData.isOutlined = true;
}
yield return new WaitForSeconds(15f);
if (enemyObject != null) {
enemyData.isOutlined = false;
}
catsEyeActive = false;
break;
}
}
The Issue
Is that even with the player.cateyeLevel set at 0 the ability is acting as if it's at level 3 and is highlighting everything. The player.cateyeLevel refers to a player script but there are no variables set there that would be the cause of the issue, in fact, if anything the player.cateyeLevel variable for this ability has been set to 0. There's also no PlayerPrefs storing the player.cateyeLevel so it isn't that either.Anyone got any ideas?
Thanks in advance.
Answer by TwistedDevelopments · Nov 15, 2021 at 12:30 PM
Resolved the issue by rewriting the the functionality entirely, the new code for anyone interested uses Update for the main bulk of the outlining and the coroutine is only there for timing now.
void Update()
{
enemyObjects = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemyObject in enemyObjects){
enemyData = enemyObject.GetComponent<Enemy>();
if (enemyObject != null) {
if (catsEyeActive && player.cateyeLevel == 0) {
enemyData.isOutlined = true;
}
}
}
items = GameObject.FindGameObjectsWithTag("Item");
foreach(GameObject itemObject in items){
item = itemObject.GetComponent<Item>();
if (item != null) {
if (catsEyeActive && player.cateyeLevel == 1) {
enemyData.isOutlined = true;
item.isOutlined = true;
}
}
}
specials = GameObject.FindGameObjectsWithTag("Special");
foreach(GameObject specialObject in specials){
special = specialObject.GetComponent<Special>();
if (special != null) {
if (catsEyeActive && player.cateyeLevel == 2) {
enemyData.isOutlined = true;
item.isOutlined = true;
special.isOutlined = true;
}
}
}
hiddens = GameObject.FindGameObjectsWithTag("Hidden");
foreach(GameObject hiddenObject in hiddens){
hidden = hiddenObject.GetComponent<Hidden>();
if (hidden != null) {
if (catsEyeActive && player.cateyeLevel == 3 ) {
enemyData.isOutlined = true;
item.isOutlined = true;
special.isOutlined = true;
hidden.isOutlined = true;
}
}
}
}
public void CatsEye(){
if (player.cateye >= 1 && !catsEyeActive) {
player.cateye -= 1;
StartCoroutine("CatEye");
}
}
IEnumerator CatEye(){
catsEyeActive = true;
yield return new WaitForSeconds(15f);
catsEyeActive = false;
}
Answer by FlippyPls · Nov 13, 2021 at 06:26 PM
Have you tried printing the cateyeLevel inside the coroutine to make absolutely sure it's not getting the wrong value?
Also: I don't see how it would change anything, but I'd generally pass it in as an argument to the coroutine as well rather than just grabbing the variable inside the coroutine. At least in cases where you wouldn't want your initial value to change throughout your coroutine since you're working with delays.
StartCoroutine(CatEye(player.cateyeLevel));
IEnumerator CatEye(int currentLevel){
very cool code
}
Tried print and a debug.log both say that the Level is 0 but it still acts as though it's at 3. I've even cleared PlayerPrefs in case I had drunkenly saved the ability level there at some point and it still says 0 in print and Debug but acts like level 3 :(