Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Script works on C# but not works on Javacript

Discussion in 'Scripting' started by fabiobh, May 6, 2014.

  1. fabiobh

    fabiobh

    Joined:
    May 28, 2013
    Posts:
    92
    Hi, I purchase a plugin to help with android development.

    I use the following line to show ads in my app

    Code (csharp):
    1. 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?
     
  2. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    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.
     
  3. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    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).
     
    Last edited: May 6, 2014
  4. fabiobh

    fabiobh

    Joined:
    May 28, 2013
    Posts:
    92
    I try to put in the standard folder, but give the same error
     
  5. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,957
    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.
     
    Last edited: May 6, 2014
  6. fabiobh

    fabiobh

    Joined:
    May 28, 2013
    Posts:
    92
    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
     
  7. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    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.
     
  8. fabiobh

    fabiobh

    Joined:
    May 28, 2013
    Posts:
    92
    I'll try to do that, thanks
     
  9. MarkrosoftGames

    MarkrosoftGames

    Joined:
    Jan 5, 2012
    Posts:
    442
    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.
     
  10. fabiobh

    fabiobh

    Joined:
    May 28, 2013
    Posts:
    92
    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?
     
  11. MarkrosoftGames

    MarkrosoftGames

    Joined:
    Jan 5, 2012
    Posts:
    442
    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
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6.  
    7. public class GameManager : MonoBehaviour {
    8.  
    9.  
    10.     private TitleScreen titleScreen;
    11.     [B]private GameObject ballServer;[/B]
    12.  
    13.  
    14.  
    15.     // Use this for initialization
    16.  
    17.     void Start () {
    18.         GameEventManager.InTheBeginning += InTheBeginning;
    19.         GameEventManager.GamePlayStart += GamePlayStart;
    20.         GameEventManager.GamePlayOver += GamePlayOver;
    21.  
    22.  
    23.         titleScreen = GameObject.Find("TitleScreen").GetComponent<TitleScreen>();
    24. [B]        ballServer = GameObject.FindGameObjectWithTag ("BallServer");
    25. [/B]
    26.  
    27.  
    28.  
    29.  
    30.         GameEventManager.TriggerInTheBeginning();
    31.  
    32.  
    33.     }
    34.  
    35.  
    36.     // Update is called once per frame
    37.     void Update () {
    38.  
    39.  
    40.     }
    41.  
    42.  
    43.     void InTheBeginning()
    44.     {
    45.         // activate title screen
    46.         titleScreen.Activate();
    47.         [B]ballServer.SendMessage ("BallServerDeActivate");[/B]
    48.  
    49.         //ballServer.BallServerDeActivate();
    50.         // ball server disable
    51.     }
    52.  
    53.  
    54.     void GamePlayStart()
    55.     {
    56.         Debug.Log ("GamePlayStart Triggered");
    57.         titleScreen.DeActivate();
    58.         // activate gameplay
    59.  
    60.  
    61.         [B]ballServer.SendMessage ("BallServerActivate");[/B]
    62.  
    63.         //ballServer.BallServerActivate();
    64.     }
    65.  
    66.  
    67.     void GamePlayOver()
    68.     {
    69.         [B]ballServer.SendMessage ("BallServerDeActivate");[/B]
    70.  
    71.         // activate gameover screen
    72.     }
    73. }
    74.  
    BallServerScript.boo
    Code (csharp):
    1.  
    2. import UnityEngine
    3. /*
    4. Serves a ball either left or right at a random angle if no ball is in play
    5. */
    6. class BallServerScript (MonoBehaviour):
    7.  
    8.  
    9.     public ballServerActive as bool = false
    10.  
    11.  
    12.     public ballToServe as BallScript
    13.     public serveForce as single = 200
    14.     public ballInPlay = false
    15.     directionToServe = -1.0F
    16.     # on x axis, -1.0F for right, 1.0F for left
    17.  
    18.  
    19.     def Start ():
    20.         pass
    21.  
    22.  
    23.     def Update ():
    24.         if not ballInPlay and ballServerActive:
    25.             // determine angle
    26.             angle = Random.Range(30.0F, 150.0F)
    27.             transform.Rotate(Vector3(0,0,angle*directionToServe))
    28.             ball as BallScript = Instantiate(ballToServe, transform.position, transform.rotation)
    29.             ball.rigidbody.AddRelativeForce(Vector3.up * 200)
    30.             transform.Rotate(Vector3(0,0,angle*directionToServe*-1.0F))
    31.             ballInPlay = true
    32.  
    33.  
    34.     def SetDirectionRight():
    35.         directionToServe = -1.0F
    36.  
    37.  
    38.     def SetDirectionLeft():
    39.         directionToServe = 1.0F
    40.  
    41.  
    42.     public def BallServerActivate():
    43.         ballServerActive = true;
    44.  
    45.  
    46.     public def BallServerDeActivate():
    47.         ballServerActive = false;
    48.  
    49.  
    50.  
    51.  
     
  12. fabiobh

    fabiobh

    Joined:
    May 28, 2013
    Posts:
    92


    In the code that you show, you send a message from C# to Boo, But I want the opposite, send a message from Javascript to C#. But I think I understand, I'll try
     
  13. TheMeanCoder

    TheMeanCoder

    Joined:
    Apr 25, 2014
    Posts:
    15
    Put AndroidAdMobController into a folder called Plugins
    You might need to create that folder.
     
  14. MarkrosoftGames

    MarkrosoftGames

    Joined:
    Jan 5, 2012
    Posts:
    442
    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?
     
  15. fabiobh

    fabiobh

    Joined:
    May 28, 2013
    Posts:
    92
    I already have the plugins folder, I try to move the script to it, but it not works.