- Home /
Problem with mysql between CS and JavaScript
Hello.
Ive got my CS mysqlscript running. Ive created a class
public class mysqlconn : MonoBehaviour {
string Start () {
string row = "";
........
return row;
}
and a Javascript file:
var text = mysqlconn.Start();
GUI.Label (Rect (10, 10, 100, 20), text);
But the Javascript file tells me that mysqlconn is an uknown identifier. what is my problem? Isnt it possible to give the variable row to the javascript file?
Second question: Do these files need to be in the same folder and attached to the same object?
Thanks
Answer by qJake · May 25, 2010 at 08:56 AM
You can't just use "mysqlconn" out of nowhere, you didn't instantiate an instance of it.
You need to do one of three things:
- Either have a game object with your MySQL script on it, and then get a reference to the MySQL script off that game object and use that, OR
- Make your MySQL class public (don't derive it from MonoBehaviour), and instantiate an instance of it before trying to use it (
mysqlconn mysql = new mysqlconn();
) (these are basic programming concepts), OR - Make the methods in your MySQL class static. I don't know how you've coded your class, so I can't say for certain whether or not this will work, but it may.
I recommend option 2.
Also, I wouldn't put the MySQL query inside of an OnGUI() function (which it appears you may have done), unless you want your database to get hammered and your game to run really slow.
Answer by Extrakun · May 25, 2010 at 08:48 AM
Javascript are complied first before C#, so you have to code that JavaScript snippet in C#, or change the compilation order so that your C# code will compile first. There are some folder where you can place your C# script so it will be complied first. Refer to the documentation here.
Why do you think this isn't the problem SpikeX?... looks like it to me. (although I always welcome being proven wrong!).
Unless there's some code that he didn't post, he's trying to access a method of a class that he didn't instantiate! Obviously that's not going to work. Script compilation order is completely irrelevant here.
Both of you are right. If they're in the same folder, he won't be able to use the type from JavaScript. But using the class name for an instance method isn't going to work either.
I wanted to upvote this answer, as it's true, the OP probably has two problems - instance not created, and wrong compilation order. But - this Answer says, "you have to code your JS in C#", which is incorrect. It's then followed by a link on how to fix compilation-order, which is why the first part is incorrect. A bit confusing, maybe you need to make it more clear that the problem and fix, is changing the order of script compilation. Not re-writing code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023636im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Help with my double jump script 1 Answer
Physics: Is it possible to count the number of colliders hit with OverlapSphere? 1 Answer