Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 07, 2015 at 10:27 AM by Raresh for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Raresh · Feb 21, 2015 at 07:36 PM · c#dictionarydatabase handling

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karljj1 · Feb 21, 2015 at 07:38 PM 1
Share

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.

avatar image Raresh · Feb 21, 2015 at 07:49 PM 0
Share

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...

avatar image karljj1 · Feb 21, 2015 at 07:51 PM 1
Share

What do you mean invoke the classes?

Are these instances of classes or classes defined within the class? Post your code.

avatar image Raresh · Feb 21, 2015 at 08:00 PM 0
Share
 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.

avatar image pako · Feb 21, 2015 at 08:02 PM 0
Share

Are the 100 classes in DB public?

Show more comments

4 Replies

  • Sort: 
avatar image
1
Best Answer

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);
  }
Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karljj1 · Feb 21, 2015 at 08:32 PM 1
Share

$$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.

avatar image karljj1 · Feb 21, 2015 at 08:47 PM 1
Share

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" );
avatar image karljj1 · Feb 21, 2015 at 08:57 PM 1
Share

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

avatar image karljj1 · Feb 22, 2015 at 05:35 PM 1
Share

Is the element name exact? Its case sensitive. Post your code

avatar image karljj1 · Feb 22, 2015 at 06:50 PM 1
Share

Can you post the ChemDB code? Also what is the value you have in element?

Show more comments
avatar image
1

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;
     }

 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges