x


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

more ▼

asked May 25 '10 at 07:37 AM

Tobias gravatar image

Tobias
378 50 54 66

@Tobias, your second question is addressed by @Extrakun's Answer - when you use two different languages in Unity, you have to work on compilation order. Does that mean you applied @SpikeX's Answer, and then found a second problem? Have you gotten it completely working, and how?

May 26 '10 at 02:51 PM Cyclops
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 25 '10 at 08:56 AM

qJake gravatar image

qJake
11.6k 43 78 161

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 25 '10 at 08:48 AM

Extrakun gravatar image

Extrakun
1.3k 44 56 70

No, this isn't the problem at all.

May 25 '10 at 08:53 AM qJake

Why do you think this isn't the problem SpikeX?... looks like it to me. (although I always welcome being proven wrong!).

May 25 '10 at 08:57 AM duck ♦♦

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.

May 25 '10 at 10:24 AM qJake

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.

May 25 '10 at 11:09 AM Mike 3

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.

May 25 '10 at 03:08 PM Cyclops
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5096
x3465
x102

asked: May 25 '10 at 07:37 AM

Seen: 1589 times

Last Updated: May 25 '10 at 07:37 AM