Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by JaRo234 · Jan 17, 2018 at 11:27 AM · variablesaccessmethod

Get variables from another script method loop

Hi there,

I have a method that returns random data from "drivers/users" table in my Database.cs

Database.cs

 public void getRandomDriver()
     {
 
         List<string> fields = new List<string>();
         fields.Add("ID");
         fields.Add("name");
 
         SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair();
         condition.fieldName = "ID";
         condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
         condition.value = "2"; // TODO: random ID
 
         DBReader reader = db.Select("drivers", fields, condition);
         while (reader != null && reader.Read())
         {
             string **driverID** = reader.GetStringValue("ID");
             string **driverName** = reader.GetStringValue("name");
         }
 
     }

Another.cs

 Database database;
 
 void getRandomDriver()
     {
         database = GameObject.Find("Database").GetComponent<Database>();
 
         database.getRandomDriver();
         //get those variables here
 
     }

I would like to access driverID and driverName from Database.cs getRandomDriver() method. How can I do that?

Could you at least point me direction?

I do not have much knowledge about C# but I have to create a prototype and I have no time for basics (in a perfect world I would have).

Thank you.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by Mehul-Rughani · Jan 17, 2018 at 12:48 PM

Hello, @JaRo234

Database.cs

 public string getRandomDriver ()
     {
         
         List<string> fields = new List<string> ();
         fields.Add ("ID");
         fields.Add ("name");
         
         SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair ();
         condition.fieldName = "ID";
         condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
         condition.value = "2"; // TODO: random ID
         
         DBReader reader = db.Select ("drivers", fields, condition);
         string driverIdAndName = "";
         while (reader != null && reader.Read()) {
             driverIdAndName += reader.GetStringValue ("ID") + "$";//You can Put any special character you want
             driverIdAndName += reader.GetStringValue ("name");
         }
         return driverIdAndName;
     }

and

Another.cs

 Database database;
     
     void getRandomDriver ()
     {
         database = GameObject.Find ("Database").GetComponent<Database> ();
         
         string driverIdAndName = database.getRandomDriver ();
         string driverId = driverIdAndName.Substring (0, driverIdAndName.IndexOf ("$"));
         string driverName = driverIdAndName.Substring (driverIdAndName.IndexOf ("$") + 1);
         
     }


Hope you are looking for this. :)

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 Hellium · Jan 17, 2018 at 02:14 PM

You should have a Driver class in order to hold those data IMHO.

 // Driver.cs
 public class Driver
 {
     public string Name { get ; private set ; }
     public string ID { get ; private set ; }
     public Driver( string id, string name ) { ID = id ; Name = name ; }
 }

   // Database.cs
  public Driver getRandomDriver()
  { 
      List<string> fields = new List<string>();
      fields.Add("ID");
      fields.Add("name");
 
      SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair();
      condition.fieldName = "ID";
      condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
      condition.value = "2"; // TODO: random ID
 
      DBReader reader = db.Select("drivers", fields, condition);
      while (reader != null && reader.Read())
      {
          return new Driver( reader.GetStringValue("ID"), reader.GetStringValue("name") ) ;
      } 
      return null ;
  }

 // Another.cs

  Database database;
  
  void getRandomDriver ()
  {
      database = GameObject.Find ("Database").GetComponent<Database> ();
      
      Driver driver = database.getRandomDriver ();
      if( driver != null )
          Debug.Log( driver.ID + " " + driver.Name ) ;         
  }


Other possibility, using out parameter

   // Database.cs
  public void getRandomDriver( string out ID, string out name )
  { 
      ID = string.Empty;
      name = string.Empty;
      List<string> fields = new List<string>();
      fields.Add("ID");
      fields.Add("name");
 
      SQLiteDB.DB_ConditionPair condition = new SQLiteDB.DB_ConditionPair();
      condition.fieldName = "ID";
      condition.condition = SQLiteDB.DB_Condition.EQUAL_TO;
      condition.value = "2"; // TODO: random ID
 
      DBReader reader = db.Select("drivers", fields, condition);
      while (reader != null && reader.Read())
      {
          ID = reader.GetStringValue("ID") ;
          name = reader.GetStringValue("name") ) ;
      } 
  }

 // Another.cs

  Database database;
  
  void getRandomDriver ()
  {
      database = GameObject.Find ("Database").GetComponent<Database> ();
      
      string ID;
      string name ;
      database.getRandomDriver ( out ID, out name );
      Debug.Log( ID + " " + Name ) ;         
  }

Comment
Add comment · Show 1 · 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 JaRo234 · Jan 17, 2018 at 02:23 PM 0
Share

@Hellium Thank you as well. I will definitely check out those out parameters.

avatar image
0

Answer by JaRo234 · Jan 17, 2018 at 02:21 PM

@Mehul-Rughani Thank you so much!

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

75 People are following this question.

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

Related Questions

Unity 3.1 Webplayer: access method failed 1 Answer

problems accessing variables and functions in another script 1 Answer

Webplayer exception when using BufferedStream 0 Answers

Access variables of another scene? 2 Answers

Access all variables of type within a C# script 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