1

I am a bit stuck with my game. I have a class called Upgradebuttons. From this class I want to access some variables stored in a struct from another class. I can easily access the variables by typing classname.structname.preferedvar but the structname depends on which upgrade as been clicked. So I want to call the struct using a string. I have tried:

       MethodInfo method = typeof(Classname).GetMethod(structname);

But this only works if it is a void and not a struct. What do I need to do in order to get this working?

    public class UpgradeButtons : MonoBehaviour {

       public void somefunction{
          // here i want to have access

      }

 }

This is an example of the class I want to have access to:

    public class Upgrades: MonoBehaviour {
        public struct Upgrade1{
            public const int Cost = 10;
            public const float Value = 0.1f;
            public static string Naam = "Autoclicker";
       }
 }

3 Answers 3

4

While this is possible, it sounds like poorly thought out design. Perhaps you could use one common struct Upgrade and use the Name property to find it at runtime?

Example:

public struct Upgrade
{
    public string Name;
    public int Cost;
    public float Value;

    public Upgrade(string name, int cost, float value)
    {
        this.Name = name;
        this.Cost = cost;
        this.Value = value;
    }
}

public class UpgradeButtons : MonoBehavior
{
    List<Upgrade> Upgrades = new List<Upgrade>();

    public void CreateButtons()
    {
        Upgrades.Add(new Upgrade("Autoclicker", 10, 0.1f));
        //etc...
    }

    public void somefunction()
    {
        Upgrade autoclickUpgrade = Upgrades.Where(p => p.Name == "Autoclicker").FirstOrDefault();

        if(autoclickUpgrade == null)
            throw new Exception("Could not find Autoclicker upgrade.");

        //do something with autoclickUpgrade
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

just remove static from string Naam:

 public class Upgrades: MonoBehaviour {
        public struct Upgrade1{
            public const int Cost = 10;
            public const float Value = 0.1f;
            public string Naam = "Autoclicker";
       }

 }

When an object ( class, variable, method) is defined as static it can not be referenced through an instance.

Comments

1

If I understand your question, you are on the right track, probably. Since the struct (type) that your add-on depends on may or may not exist, Reflection is the best way to go here.

Try Assembly.GetType(), or a related method, to attempt to load the type, check its existence, and iterate its members.

http://msdn.microsoft.com/en-us/library/y0cd10tb(v=vs.110).aspx

3 Comments

Thank you both for the quick respond @mrjoltcola & @apomene! I removed the static and added: Assembly assem = typeof(Upgrades).Assembly; Type m_type = assem.GetType("Upgrades.Upgrade1"); To the upgrade button script. However, I do not get any errors but when I Debug.Log(m_type) it does not log anything. When I type m_type. it does not return those variables.
I'm confused now, you accepted an answer that doesn't load anything dynamically, so did you post this comment before or after that?
You are right but he told me "it sounds like poorly thought out design." So I thought maybe I can better change it to his suggestion and from there move further along.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.