Search Unity

quick question about between C# and Javascript...

Discussion in 'Scripting' started by alienx2, Apr 1, 2011.

  1. alienx2

    alienx2

    Joined:
    Nov 19, 2010
    Posts:
    40
    Is there possible that C# can change value of variable from javascript which create own variable early?

    then also Javascript can change variable from C# too?

    thanks.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    In javascript and C# you reference the script as an object.

    var script=otherobject.gameObject.GetComponent("ScriptName");

    so script.myValue is a value in that script.

    C# is similar but you have to typecast the GetComponent

    ScriptName script=otherobject.gameObject.GetComponent<ScriptName>();

    I think, do a search on GetComponent in the forum... or in the documentation.
     
  3. alienx2

    alienx2

    Joined:
    Nov 19, 2010
    Posts:
    40
    but... i have problem...my game is almost mixing script between C# and Javascript... =/

    javascript.js file
    Code (csharp):
    1.  
    2. static var TEXT : String;
    3. ....
    4. ....
    5.  
    CSharp.cs file
    Code (csharp):
    1.  
    2. void Start() {
    3. ....
    4. javascript.TEXT = "hello";
    5. ....
    6. }
    7.  
    ERROR:
    Code (csharp):
    1.  
    2. error CS0103: The name `javascript' does not exist in the current context
    3.  
     
  4. CheyTac

    CheyTac

    Joined:
    Feb 17, 2011
    Posts:
    71
    CSharp.cs
    you need to make the function global, im not sure but i think it may work, im noob :D
     
  5. alienx2

    alienx2

    Joined:
    Nov 19, 2010
    Posts:
    40
    yep look at javascript.js... that's global variable.. i only want CSharp going change variable from javascript.js.

    hehehe me too im noobs for C# only but im learning :)
     
  6. alienx2

    alienx2

    Joined:
    Nov 19, 2010
    Posts:
    40
    bump....
     
  7. alienx2

    alienx2

    Joined:
    Nov 19, 2010
    Posts:
    40
    no one answer? T_T
     
  8. ahmetDP_1

    ahmetDP_1

    Joined:
    Sep 23, 2010
    Posts:
    113
    The code you wrote should work, but only in one condition. You must move your javascript code to one of the early compiled folders, such as Plugins, Standard Assets, Pro Standard Assets. Your problem is about script compilation order. Refer to the documentation here

    Script Compilation
     
  9. alienx2

    alienx2

    Joined:
    Nov 19, 2010
    Posts:
    40