C# Load a variable with a variable

Hey guys, I’m having a bit of an issue with what I would imagine would be pretty easy to deal with. I have 1 script called Stat and namespace called MasterScript with a class called PlayerStats. Inside the MasterScript.PlayerStats houses all of the possible spells and their individual levels, and each spell will have the Stat script for which I want them to load their corresponding value from MasterScript.PlayerStats.

Example: MasterScript.PlayerStats has a variable : public static int Drudge_Skeleton

In the Stat script I have 2 simple functions:

    public string _Skill_Name;
    public int _Skill_Level;
    void Start()
    {

        SkillAdjust();
        if (_Skill_Level < 1)
        {
            _Skill_Level = 1;
        }
    }

    public void SkillAdjust()
    {
//Load the corresponding variable from MasterScript.PlayerStats into _Skill_Level using _Skill_Name to find the appropriate variable
    }

The idea is that _Skill_Name = “Drudge_Skeleton”. Stat will use _Skill_Name, and load the int Drudge_Skeleton from MasterScript.PlayerStats into the _Skill_Level.

Hopefully this made sense

To answer your question with a single word, it’s possible with reflection.

However that’s not the way to go. You have a “bad design” in OOP sense. C# (and basically even UnityScript) is a statically typed language. What you want is something that usually only dynamic / interpreted scripting languages support.

A variable name is ment to describe what it contains. The name “Drudge_Skeleton” doesn’t seem to describe what that “int” actually represents. As soon as you want to add more variations of a variable which actually contains the same kind of information but a different variation you most likely want to use a class instead. For example you never create variables like healthEnemy1 and healthEnemy2. You should have an Enemy class which just contains a health variable. Now you can have two instances of that class and each can have different values.

So if you have many different skill levels, each level should be represented by an instance of a skill class. All skills would use the same class but have different values.

Example:

// Skill.cs
[CreateAssetMenu(fileName ="skill.asset", menuName ="Create New Skill")]
public class Skill : ScriptableObject
{
    public int level;
    public int maxHP;
    public int someOtherStatValue;
}

Now you can simply create different skill assets with the “create” menu in Unity. Give each skill a name and set the values you like.

Now in another script you can simply create a List or Array of "Skill"s and drag&drop all your skill assets into the array:

public Skill[] skills;

ScriptableObjects also have a “name” property to actually get the name of a Skill instance.

You should avoid “static” whenever possible. static limits you in the usage and is only ment for “static references”. As soon as you want anything dynamic, forget about static. Besides that static variables are essentially global state which you generally want to avoid. Singletons are just a variation of it.

If you want to access public variable or methods from Script_2 in Script_1 you need a reference (variable) of the instance of Script_2 that you want to use (remember that, like any object, a script can have multiple instances of itself, with different values for its attribute in each instance).
To do that do something like this in Script_1:

`public Script_2 myInstanceOfScript_2;

void Start() {

myInstanceOfScript_2.methodFromScript_2();

}`

But in your case I guess that there will only be one single instance of MasterScript at any given time. If so, I recommande having a look to the Singleton design patern. In Unity it can be use to create global scripts that can be access from anywhere without the need of an instance (like a static class but better): Singleton

Your question does not make a lot of sense.

But what I can gather is you want to retrieve a value with a string simple way:

public enum SkillName{
    Drudge_Skeleton,
    Fat_Skeleton,
}

public void SkillAdjust(SkillName skillNameEnum, string skillNameString)
{
    //Using Enum
    if(skillNameEnum== SkillName.Drudge_Skeleton)
        _Skill_Level = MasterScript.PlayerStats.Drudge_skeleton;

    //OR String
    if(skillNameString== "Drudge_Skeleton")
         _Skill_Level = MasterScript.PlayerStats.Drudge_skeleton;

}

But this is a quick and dirty way, There are more advance ways by using classes and calling the class that you want by using a dictionary. I would consider re-structuring your code.