Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Access script from Javascript to C# (OK) but from C# to Javascript (error)

Discussion in 'Scripting' started by petero181, Mar 18, 2012.

  1. petero181

    petero181

    Joined:
    Feb 23, 2011
    Posts:
    104
    Hi, I try to search thread about accessing script from Javascript to C# and I found it ... and now Its run perfect. And I search again the thread about accessing script from C# to Javascript ... there comes a problem.

    In my search said that I just place C# files into Standard Assets so it will compile first. But now when I need to accessing script from C# to Javascript ... what will I do ?

    This codes below have no problem
    in Player.js :
    Code (csharp):
    1.        
    2. var theAxe : GuiManager = gameObject.GetComponent(GuiManager);
    3. theAxe.Axe(true);           // GuiManager.Axe
    4.  
    in GuiManager.cs :
    Code (csharp):
    1.    
    2. public void Axe(bool theData) {
    3.     guiAxeToggle.selected = theData;
    4.     //Debug.Log("Received communication from Player-theAxe");
    5. }
    This codes below has problem :
    in GuiManager.cs :
    Code (csharp):
    1.     Player playerAxe = gameObject.GetComponent<Player>();
    2.     playerAxe.AxeActivate(true);
    3.  
    in Player.js :
    Code (csharp):
    1. public function AxeActivate(theData : boolean) {
    2.     Axe.SetActiveRecursively(theData);
    3.     AxeCollider.SetActiveRecursively(theData);
    4.     CurrentWeapon = AxeCollider;
    5.  
    6. }
    error CS0246: The type or namespace name 'Player' could not be found. Are you missing a using directive or an assembly reference?

    Please help ... what should I do. Thanks in advanced. I know its bad to mixing languages.
     
    Last edited: Mar 18, 2012
  2. petero181

    petero181

    Joined:
    Feb 23, 2011
    Posts:
    104
    sorry ... i forgot to mention. this two scripts are attach to one gameObject
     
  3. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    You can't help it - one of them has to get built first, and so they can't both access types in each other.

    There are a lot of workarounds, most of which are rather ugly. The easiest way is probably to use Unity's SendMessage support, but it's discouraged.

    One elegant way would be to declare interfaces in C# in Plugins (or StandardAssets if you like), and make your C# classes implement those interfaces. Then the C# component can access the JS component directly, and the JS component can access the C# component through the interface. You'll need the C# component to pass itself to the JS component first, though, because the JS component will still not be able to use GetComponent to find it.

    Or you could just use a delegate to make your JS allow the C# script to tell it how to call the method - i.e. in the JS:

    Code (csharp):
    1.  
    2. public System.Action.<bool> AxeMethod;
    3.  
    4. ...
    5.     AxeMethod(true);
    6. ...
    7.  
    And the C# needs to register its method:

    Code (csharp):
    1.  
    2. GetComponent<Player>().AxeMethod = this.Axe;
    3.  
    This way the JS doesn't need to know about the C# at compile time, so you can just put all the files in the regular Assets folder.

    Obviously the JS needs to not call the method before it's initialised. If JS can do events then they might be a better model.
     
  4. petero181

    petero181

    Joined:
    Feb 23, 2011
    Posts:
    104
    thx George for your quick reply. I just try and still got the same error on :

    Code (csharp):
    1. GetComponent<Player>().AxeMethod = this.Axe;
    error CS0246: The type or namespace name `Player' could not be found. Are you missing a using directive or an assembly reference?

    what shoud I do?
     
    Last edited: Mar 20, 2012
  5. petero181

    petero181

    Joined:
    Feb 23, 2011
    Posts:
    104
    Btw ... I try to use SendMessage from C# to Js and its success. So I stick with SendMessage until I found other way to do accessing Js from C# ... thx anyway
     
  6. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Don't mix C# and UnityScript in your project is the best solution. Purely C# or purely UnityScript don't have this problems.

    C# is compiled first, then UnityScript. This is the reason why you can access the C# script from UnityScript but not vice-versa, because at the time time the C# assembly is compiled the UnityScript one isn't compiled yet so C# can't find it