Home > OS >  Unity can't find MySql namespace or type how do I fix it?
Unity can't find MySql namespace or type how do I fix it?

Time:10-31

I'm Trying to connect my unity game to a MySql database, I keep getting these errors even tho i've downloaded the MySql.Data package.

Here is my code:



your text

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using TMPro;
using MySql.Data.MySqlClient;

public class DBConnection : MonoBehaviour
{
    public TMP_Text Message;

     void Connect()
    {

        var connectionString = "server=localhost;Database=database;Uid=root;";

        using (var connection = new MySqlConnection(connectionString))
        {
            connection.Open();

            using var command = new MySqlCommand("SELECT COUNT(*) FROM autoincrement", connection);

            var Count = command.ExecuteScalar();

            connection.Close();

            Message.text = $"There are {Count} people";

        }

        
    }
}

This is the error that i get And this

I've tried uninstalling and reinstalling the package multiple times, but nothing works, this same code works in any other C# script I make in VS but as soon as I do it in unity it doesn't work.

CodePudding user response:

The solution is:

Copy the file “MySql.Data.dll” from c:\Program Files (x86)\MySQL\MySQL Connector Net 6.0.3\Assemblies\ to the Assets folder of your project (or even better to Assets/Plugin folder or Library folder)

and change: Player Settings-->Optimization-->Api Compatibility Level = .NET 2.0

Similar problem: https://answers.unity.com/questions/29293/compiler-error-with-mysql-connection-in-unity-3-sy.html

May help to continue development with it: https://github.com/Uncle-Uee/mysql-unity

  • Related