I need a fresh set of eyes on this script please.

Ive received the code error: StatCalculations.cs(30,20): error CS0161: `StatCalculations.CalculateStat(int, StatCalculations.StatType, int, bool)': not all code paths return a value

Ive been looking for hours and I cant find the answer, Im sure its something simple im missing but I cant stare at this another second longer. if anyone could help Id really appreciate it :slight_smile:

using UnityEngine;
using System.Collections;

public class StatCalculations {


    private float enemSstaminaModifier = 0.25f;
    private float enemyEnduranceModifier = 0.2f;
    private float enemyIntellectModifier = 0.2f;
    private float enemyStrengthModifier = 0.2f;
    private float enemyDexterityModofier = 0.2f;
    private float enemyPrecisionModofier = 0.1f;
    private float enemyLuckyModofier = 0.1f;
    private float enemyMateryModofier = 0.1f;
    private float enemyCharismaModofier = 0.1f;
    private float statModifier;

    public enum StatType{
        STAMINA,
        ENDURANCE,
        INTELLECT,
        STRENGTH,
        DEXTERITY,
        PRECISIOM,
        LUCK,
        MASTERY,
        CHARISMA
    }

    public int CalculateStat(int statVal, StatType statType, int level, bool isEnemy){
        if (isEnemy) {
            SetEnemyModifier(statType);
            return (statVal + (int)((statVal * statModifier) * level));
        }
    }

    private void SetEnemyModifier(StatType statType){
        if (statType == StatType.STAMINA) {
            statModifier = enemyStrengthModifier;
        }
        if (statType == StatType.ENDURANCE) {
            statModifier = enemyEnduranceModifier;
        }
        if (statType == StatType.INTELLECT) {
            statModifier = enemyIntellectModifier;
        }
        if (statType == StatType.STRENGTH) {
            statModifier = enemyStrengthModifier;
        }
        if (statType == StatType.DEXTERITY) {
            statModifier = enemyDexterityModofier;
        }
        if (statType == StatType.PRECISIOM) {
            statModifier = enemyPrecisionModofier;
        }
        if (statType == StatType.LUCK) {
            statModifier = enemyLuckyModofier;
        }
        if (statType == StatType.MASTERY) {
            statModifier = enemyMateryModofier;
        }
        if (statType == StatType.CHARISMA) {
            statModifier = enemyCharismaModofier;
        }

    }


}
public int CalculateStat(int statVal, StatType statType, int level, bool isEnemy){
        if (isEnemy) {
            SetEnemyModifier(statType);
            return (statVal + (int)((statVal * statModifier) * level));
        }
    }

What happens if isEnemy is false? :slight_smile:

Also just a sidenote, why don’t you use else if or even a switch statement in ‘SetEnemyModifier’ ? You always check every single case at the moment whereas you could already skip the rest when something matched.

Im just learning lol Ive been following along with tutorials and tyring to piece together what ive learned. can you give me an example of what I should add to fix the error? And thank you I appreciate the help!

public int CalculateStat(int statVal, StatType statType, int level, bool isEnemy){
        if (isEnemy) {
            SetEnemyModifier(statType);
            return (statVal + (int)((statVal * statModifier) * level));
        }
   return 0;

    }
1 Like

That has eliminated the error for me but I’m seeing the warning that the value is assigned but never used. ill be able to move on with the tutorial now at least and maybe ill see something that was missing in some later videos. unless you have any idea of how to solve that issue also id love to hear it. but thanks a lot for the help brother I was starting to give up.

What line are you getting that warning on? It’s good practice to eliminate warnings!

Actually, I cleared the warning when after I sent the reply and then I closed Unity and the script and reopened them and the warning never returned so I think it might be good. Unless the warnings don’t come back after you’ve cleared them but I thought for sure they did,

They will come back if the program is re-compiled, i.e. you make a change in a script.

(7,23): warning CS0414: The private field `StatCalculations.enemSstaminaModifier’ is assigned but its value is never used

Oh I just realized that’s unrelated to the previous error lol I guess I never bothered to check the line when I saw it I just assumed it was about the change I had made.

enemy is mispelled in the variable declaration.

ive already found the mistake for that. thanks a lot for your help though. Most people don’t bother to help someone that has no Idea what theyre doing. Its very much appreciated !

Yes, warnings are mostly very explicit and easy to find/fix.

I would recommend you to utilise inheritance since it will allow you to extend your code and make it more readable for your eyes.

Try something like this:

public abstract class BaseStat
{
     public float modifier;
}

Now you can make your main stats inherit from BaseStat like:

public class StrengthStat : BaseStat
{
}
StrengthStat stat = new StrengthStat();
stat.modifier = 1.2f;

public class AgilityStat : BaseStat
{
}
AgilityStat stat = new AgilityStat();
stat.modifier = 1.6f;

// other stats go here....

Calling your calculate stat function now goes as easy as:

public int CalculateStat(int statVal, BaseStat stat, int level, bool isEnemy)
{
        if (isEnemy)
        {   
            return (statVal + (int)((statVal * stat.modifier) * level));
        }

        return 0;
    }

No more need for the eyes-bleeding switch case function. And you can make new stats on the go and the function will still work.

        CharismaStat stat = ...; //somehow get the carisma stat
        int value = this.CalculateStat(5, stat, 2, true);

Although we can’t see the rest of the code, I think that tutorial you are following might not be good since it’s not teaching you how to use design patterns. It might be hard to wrap head around them in start, but it pays off in the long run.

I absolutely agree with you that the tutorial ive been following isn’t the greatest in terms of how things are done but its one of the only ones that ive found where they take the time to explain what and why theyre doing things which is great for someone like me whos learns best from know why something is done a certain way. once I get a good grasp of all the different functions and what theyre used for then ill definitely look into more efficient ways of doing things.and thanks for that tip! ill have to play around with this a little and see what how it goes