Script works on C# but not works on Javacript

Hi, I purchase a plugin to help with android development.

I use the following line to show ads in my app

AndroidAdMobController.instance.ShowInterstitialAd();

When I use the code above in C# file(.cs) it works perfectly, but if I try to use the code above in a Javascript file (.js) the code not works.

It give the following error:

Unknown identifier: AndroidAdMobController

What I can do to correct this?

Sorry, I don’t remember javascript much someone will come along and help soon that does though. You are in the wrong forum for that question.

I’d recommend if you aren’t too far into learning coding to learn C# instead of JavaScript. If what you are doing is importing assets to glue together then those are more and more written in C#. You need to learn how to convert the original source javascript to C# is the best long term solution rather than convert working C# into javascript.

Its about where your scripts are, try putting your JS file into a folder called “StandardAssets” by default C# code is compiled first, but if you put it in the StandardAssets folder the JS file will be compiled first.

Potentially, if your plugin is in the plugins folder it won’t work(I’m not sure what comes first Standard Assets or Plugins).

I try to put in the standard folder, but give the same error

The problem is that your js script doesn’t recognize AndroidAdMobController, it’s nothing but an undeclared variable.

As goat said you’re better off learning c#… I know it’s in my todo list.

I use javascript but in c# you work with namespaces, right? And then you specify what namespace you use, I’m not sure you have that in Javascript.

Maybe you can try checking out what namespace c# is using for AndroidAdMobController, and put it before AndroidAdMobController.
For example… if it’s “using AndroidStuff”… try AndroidStuff.AndroidAdMobController in your javascript code.

Just keep in mind I don’t know much about c#, but I the problem is that your js script doesn’t recognize AndroidAdMobController.

All my app is coded in javascript, convert all game logic will be very time consuming.

I create another post in the correct forum now, thanks for the warning

javascript create a global namespace in Unity or rather the Unity engines does that…

Looking at your code:

AndroidAdMobController.instance.ShowInterstitialAd();

I’d think that javascript doesn’t know what C# namespaces are or how to access C# classes, methods, or variables isolated from the javascript namespace. As I’ve not used the AndroidAdMobController if it’s something you attach to the Main Camera for instance you need to access it then through the Main Camera as the Main Camera would contain the instance.

If you search there are threads that deal with the very special hoops you have to jump through for cross referencing classes, methods and variables across javascript C# in Unity.

I’ll try to do that, thanks

look into the SendMessage function, basically it lets you send in a string to a component to call that function. this way it is referenced at run time and not while its compiling the script. it worked for me when i had to do some c sharp and unity script stuff and didnt want to rewrite a ton of stuff.

I try to use the sendMessage function in some sites that I found, but it’s not working for me, do you have any example?

ok heres two files. one is c sharp, the other is actually a boo script and not unity script, but from my understanding the same logic should apply

edit: i tried to bold a few of the relevant lines, but it just shows up as tags when you do it inside a code tag, which makes sense lol
GameManager.cs
```
**using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

private TitleScreen titleScreen;
[B]private GameObject ballServer;[/B]



// Use this for initialization

void Start () {
    GameEventManager.InTheBeginning += InTheBeginning;
    GameEventManager.GamePlayStart += GamePlayStart;
    GameEventManager.GamePlayOver += GamePlayOver;


    titleScreen = GameObject.Find("TitleScreen").GetComponent<TitleScreen>();

ballServer = GameObject.FindGameObjectWithTag (“BallServer”);

    GameEventManager.TriggerInTheBeginning();


}


// Update is called once per frame
void Update () {


}


void InTheBeginning()
{
    // activate title screen
    titleScreen.Activate();
    [B]ballServer.SendMessage ("BallServerDeActivate");[/B]

    //ballServer.BallServerDeActivate();
    // ball server disable
}


void GamePlayStart()
{
    Debug.Log ("GamePlayStart Triggered");
    titleScreen.DeActivate();
    // activate gameplay


    [B]ballServer.SendMessage ("BallServerActivate");[/B]

    //ballServer.BallServerActivate();
}


void GamePlayOver()
{
    [B]ballServer.SendMessage ("BallServerDeActivate");[/B]

    // activate gameover screen
}

}**
** **BallServerScript.boo** **
*import UnityEngine
/

Serves a ball either left or right at a random angle if no ball is in play
*/
class BallServerScript (MonoBehaviour):

public ballServerActive as bool = false


public ballToServe as BallScript
public serveForce as single = 200
public ballInPlay = false
directionToServe = -1.0F
# on x axis, -1.0F for right, 1.0F for left


def Start ():
    pass


def Update ():
    if not ballInPlay and ballServerActive:
        // determine angle
        angle = Random.Range(30.0F, 150.0F)
        transform.Rotate(Vector3(0,0,angle*directionToServe))
        ball as BallScript = Instantiate(ballToServe, transform.position, transform.rotation)
        ball.rigidbody.AddRelativeForce(Vector3.up * 200)
        transform.Rotate(Vector3(0,0,angle*directionToServe*-1.0F))
        ballInPlay = true


def SetDirectionRight():
    directionToServe = -1.0F


def SetDirectionLeft():
    directionToServe = 1.0F


public def BallServerActivate():
    ballServerActive = true;


public def BallServerDeActivate():
    ballServerActive = false;**

```

Put AndroidAdMobController into a folder called Plugins
You might need to create that folder.

i dont recommend the strategy of moving it to another folder to force it to compile at a different time. then what happens when you need to go the other way? what other issues might be introduced by changing the compile time order as well?

I already have the plugins folder, I try to move the script to it, but it not works.