Connecting MySql in Unity3d -> NullReferenceException: Object reference not set to an instance of an object

时间:2019-03-17 22:45:44

标签: c# mysql unity3d

Here is my code in Visual Studio connected to Unity3d:

MySqlConnection connection;

MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();

                connBuilder.Server = "localhost";
                connBuilder.UserID = "root";
                connBuilder.Database = "therapygame";
                connBuilder.Password = "";
                connBuilder.OldGuids = true;

                connection = new MySqlConnection(connBuilder.ConnectionString);

Here is the error in Unity:

NullReferenceException: Object reference not set to an instance of an object
System.Data.Common.DbConnectionStringBuilder.Remove (System.String keyword)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.Remove (System.String keyword)
MySql.Data.MySqlClient.MySqlConnectionStringOption.Clean (MySql.Data.MySqlClient.MySqlConnectionStringBuilder builder)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.SetValue (System.String keyword, System.Object value)
MySql.Data.MySqlClient.MySqlConnectionStringOption.<.ctor>b__0 (MySql.Data.MySqlClient.MySqlConnectionStringBuilder msb, MySql.Data.MySqlClient.MySqlConnectionStringOption sender, System.Object value)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item (System.String keyword, System.Object value)
MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Server (System.String value)
mysql.SetupSQLConnection () (at Assets/mysql.cs:31)
fire_rate.Start () (at Assets/fire_rate.cs:18)

Line 31 of mysql.cs:

connBuilder.Server = "localhost";

Line 18 of fire_rate.cs:

GameObject.Find("MySQL").GetComponent<mysql>().SetupSQLConnection();

This post is an update on a previous issue. Changes in code were made via an answer on that post. Here is the link: MySqlConnection = new MySqlConnection(string) not working

1 个答案:

答案 0 :(得分:0)

这行得通!

  

获得NullReferenceException(这很奇怪   通常表示错误)在DbConnectionStringBuilder.Remove(   框架库)调用   MySqlConnectionStringBuilder.set_Server(这是很常见的   属性)。 Unity是否提供该库的自己的版本,我   奇迹?如果可以,请尝试切换到nuget.org/packages/MySqlConnector   MySQL的替代OSS驱动程序,因为它不使用相同的驱动程序   您的异常消息显示的ConnectionStringBuilder代码路径。

@Bradley Grainger