- Home /
The question is answered, right answer was accepted
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)
{
Perioada=tPerioada;
Grupa=tGrupa;
NrAt=tNrAt;
Simbol=tSimbol;
Masa=tMasa;
Valenta=tValenta;
Tip=tTip;
Stare=tStare;
a=ta;
b=tb;
c=tc;
d=td;
e=te;
f=tf;
g=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.
Have a reference to DB in your orbit script which you can then assign in the inspector. $$anonymous$$G
public DB myReference;
will now appear on the script in the inspector so you can drag the DB into its slot.
Yeah i tried that too but when i say something like myReference.Num50 it doesn't show up. How do i invoke the classes from DB, that's what i need... i also tried some other methods too from other threads but i can't say they worked...
What do you mean invoke the classes?
Are these instances of classes or classes defined within the class? Post your code.
public class DB : $$anonymous$$onoBehaviour
{
void Start ()
{
Element Numfifty=new Element(7,18,118,"Num50","U",294,0,"$$anonymous$$","$$anonymous$$",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 $$anonymous$$asa;
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 t$$anonymous$$asa,int tValenta,string tTip,string tStare,int ta,int tb,int tc,int td,int te,int tf,int tg)
{
Perioada=tPerioada;
Grupa=tGrupa;
NrAt=tNrAt;
Simbol=tSimbol;
$$anonymous$$asa=t$$anonymous$$asa;
Valenta=tValenta;
Tip=tTip;
Stare=tStare;
a=ta;
b=tb;
c=tc;
d=td;
e=te;
f=tf;
g=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.
Answer by karljj1 · Feb 21, 2015 at 08:03 PM
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);
}
$$anonymous$$ake sure you actually assign something to chemDB. This means dragging the object into the slot in the inspector. It could also be that Numfifity is null if your trying to print the value before the Start function has been called.
Whats the error preventing you from Starting the scene?
What is null, is it chemDB or Numfifty?
$$anonymous$$G
if( chemDB == null )Debug.Log( "Its chemDB" );
else if( chemDB.Numfifty == null )Debug.Log( "Its Numfifty" );
Use a dictionary.
$$anonymous$$G
public Dictionary<string,Element> myClasses = new Dictionary<string,Element>();
Then
myClasses["Numfifty"] = new Element(.......
To get the value
Element anElement = myClasses["Numfifty"];
https://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx
Is the element name exact? Its case sensitive. Post your code
Can you post the ChemDB code? Also what is the value you have in element?
Answer by Stardog · Feb 21, 2015 at 08:02 PM
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;
}
}
Answer by vintar · Feb 21, 2015 at 08:10 PM
Maybe something like this ?
DB db = GetComponent<DB>();
db.NUM50 num50 = new db.NUM50();
a bit messy, maybe someone will have an easier method :)
Answer by Kerihobo · Feb 22, 2015 at 09:11 PM
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.
soooo....
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;
or
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
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Changing material color from dictionary (C#) 0 Answers
How do I create a Key and Values Dictionary array in C# 1 Answer
Combine Children Dictionary in place of Hashtable? 2 Answers