# Using Variable to Reference Different C# Script

I had a question about using a variable to access different scripts. I have a Rhythm game I'm working on, and I have different scripts named Rudement1, Rudement2, Rudement3, ect... One of my other scripts has something like this,

public GameObject HighHatFlare;

// Use this for initialization
void Start ()
{
Rudement1.NoteCreated = Rudement1.NoteCreated + 1;
}


What I'm looking to do is use this same script but modify it to to something like this:

public GameObject HighHatFlare;

// Use this for initialization
void Start ()
{
Rudement(RudementNumber).NoteCreated = Rudement(RudementNumber).NoteCreated + 1;
}


The goal is that I can continually use this script and it will modify itself as I use different "Rudement" scripts. I figured it some type of "RudementNumber" or 'RudementNumber', but I can't figure it out. Can someone help me with this?

A rudement is a basic sticking pattern that you learn at a very beginning level of drumming. Also spelt "Rudiment" I was always told to spell it with an "e" because of its French roots (not relevant).

In this context it could be replaced by the term "level". Each rudiment is a different level and therefore has different amounts of notes. The base script between each level is identical, but the note patterns are different.The script above is attached to each note to give them a numeric order as they are created. It is running under a mono behavior. I think that at it's simplest I'm wondering if you can reference a script name via a changeable string with the intent to pull a variable from it. As the level changes so does this string to pull the amount of NotesCreated from the new level.

• First tell that are your Rudement... scripts derived from MonoBehaviour? it seems that you are relying on static variables. Second thing why there are different classes and why not same class with different variables? Can you please make it clear? – Hamza Hasan Mar 17 '16 at 16:41
• Maybe good ol Inheritance is what you need. – Savlon Mar 17 '16 at 16:43
• What exactly is a "rudement"? – Philipp Mar 17 '16 at 17:05

Whenever you create anything with identifiers named Foo1, Foo2, Foo3 etc., it is very likely that what you should actually be doing is using an array or List.

Which one is the ideal solution for your case is hard to tell because you aren't saying much about what you already have and where you want to go software-architecture wise. Also, I have no idea what a "Rudement" is. But I suspect that your "Rudement"'s are actually all subtypes of a common base-type. In that case you should have one base-class Rudement and either several instances of it which differ by their private variable values or several derived classes which implement certain methods differently.

In either case you can have a static array of "Rudements" in your base-class where you store these instances. Your Rudement class would then have a

public static List<Rudement> rudements = new List<Rudement>();


A public static list is visible everywhere in your project and can be accessed with Rudement.rudements. Any rudement which is created should add itself to that list with Rudement.rudements.Add(this);. You can then address certain rudements with Rudement.rudements[index].

This should get you going, but keep in mind that this is just one of several solutions to this general problem. There is never just one solution to a software design question and maybe a different one will suit you better.

make this variable static than you can access in any other script like public static #type of variable# myrudment in other script you can access like this name of the class in which you declared that variable #name of the class#.myrudment