Accessing a class from a different script

I have 2 C# scripts, DB and Orbit.
DB contains over 100 classes each with different names, for the purpose of this example we will name the classes Num1,Num2…Num120.

I need to access from the Orbit script one of the classes in DB, let’s say Num50. How would the syntax look for that?

Also the DB script is attached to the Main Camera (if that changes anything).

public class DB : MonoBehaviour
    void Start ()
    	Element Numfifty = new Element(7,18,118,"Num50","U",294,0,"Unknown","Unknown",2,8,18,32,32,18,8);
    public class Element
        public int Perioada;
        public int Grupa;
        public int NrAt;
        public string Nume;
        public string Simbol;
        public int Masa;
        public int Valenta;
        public string Tip;
        public string Stare;
        public int a,b,c,d,e,f,g;
        public Element(int tPerioada,int tGrupa,int tNrAt,string tNume,string tSimbol,int tMasa,int tValenta,string tTip,string tStare,int ta,int tb,int tc,int td,int te,int tf,int tg)

Numfifty is just one of the classes declared in the Start function, i just picked one of it that matches the example.

I need to get the elements from Numfifty (the values of “Perioada”,“Grupa” etc) from another script.

You have to have a variable for the class.

public class DB : MonoBehaviour {

    public class Num1
        public float number = 1f;

    public Num1 varName;


Now access it using a reference as mentioned above.

public class Orbit : MonoBehaviour {

    public DB dbReference;

    void Start()
        dbReference.varName.number = 20f;


Numfifty is not exposed, its declared in Start and so will be deleted once the Start function is exited. Do this

public Element Numfifty;
 void Start ()
     Numfifty=new Element(7,18,118,"Num50","U",294,0,"Unknown","Unknown",2,8,18,32,32,18,8);

Maybe something like this ?

DB db = GetComponent<DB>();
db.NUM50 num50 = new db.NUM50();

a bit messy, maybe someone will have an easier method :slight_smile:

say you are trying to access a class called “MyClass”, and that class has a variable:

public int myInt;

Well, if this class exists in your project folders somewhere, then that means engine can access it.


MyClass variableName = GameObject.Find("NameOfGameObjectInScene")<MyClass>();

note that GameObject.Find will run once, and if you have several objects with this name, you can’t really be sure which GameObject with that name it will actually look at.

Now, if that class is actually on the same object as the class you are trying to access it from:

MyClass variableName = GetComponent<MyClass>();

I’m pretty sure that works, otherwise you may have to make it

MyClass variableName = gameobject.GetComponent<MyClass>();

Now to actually make use of the variable, you’d go

variableName.myInt = 5;


int someNewInt = variableName.myInt;

Only PUBLIC variables can be accessed this way though. I believe if you are trying to access private variables of a componenent, you either need to trigger a public function in the receiving script, orrrr you need to make use of getters/setters