Search Unity

accessing function on other script

Discussion in 'Scripting' started by yogeez, Apr 3, 2009.

  1. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    I need to access the function which is defined on other js file, here is the brief of what the structure looks like

    This is the script i have kept in HitsScript.js file
    Code (csharp):
    1.  
    2. function OnGUI()
    3. {
    4.  
    5.    if (bButtonActive)
    6.    {    
    7.       if (GUI.Button (Rect (160,100,150,100), "Play"))
    8.       {
    9.          print("Started");
    10.          plLevel = 1;
    11.          bButtonActive = false;
    12.          rn1();
    13.          rn2();        
    14.       }
    15.    }
    16. }
    17.  
    The rn1() and rn2() functions are in the other js file called Boxchr.js and the gameObject name is also Boxchr on which the Boxchr.js is attached.

    So, its just like i need to access the function from one js file to other js file.
     
  2. horsman

    horsman

    Joined:
    Jul 19, 2008
    Posts:
    156
    make a public variable:
    Code (csharp):
    1.  
    2. var boxchrTarget : Boxchr;
    then set it in the inspector by dragging the object on.

    Finally, in the code you posted:
    Code (csharp):
    1.  
    2. function OnGUI()
    3. {
    4.  
    5.    if (bButtonActive)
    6.    {      
    7.       if (GUI.Button (Rect (160,100,150,100), "Play"))
    8.       {
    9.          print("Started");
    10.          plLevel = 1;
    11.          bButtonActive = false;
    12.          boxchrTarget.rn1();
    13.          boxchrTarget.rn2();        
    14.       }
    15.    }
    16. }
    17.  
     
  3. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    thanks horsman, but its not working and messed up with more errors. Actually how can we define
    var boxchrTarget : Boxchr;
    this doesn't make any sense. and also what about this line
    "Then set it in the inspector by dragging the object on."
    this is very confusing.

    its just the simple thing i want to ask i have made the function rn1() in Boxchr and the js file is already attached to it called Boschr, ok if the same name is causing problem to understand.. ok so

    Problem :

    Boxchr is the game object and js file attached to it is Boxchrj in this i have function rn1(). This thing i want to call from other js file on the OnGUI button function.

    i hope this time its clearly stated, previously i think i was not able to represent that much clearly.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You set it up by assigning the value in the Unity Editor through drag and drop of the corresponding object / component on the object you want to use.


    This will only work if both components use the same language or the other component is put into an "earlier to compile folder" otherwise you can not access components from other languages than the one you are working with in the script in question.

    If the JS File is Boxchrj, then thats what you need to use as type in the script for var blabla : boxchrj
     
  5. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    Drag and drop, everything is already set to drag and drop what for to do that again.
    doesn't make any sense.

    Hmmmm... Boxchr is game object and attached is the script Boxchrj in that there is function rn1();

    and now Boxsqr is other game object with attached script HitsScript. And both are at the same level (No parents or child nothing like that at all)

    And here the script is already drag dropped on the respetive game object. What to drag again to attach is much more confusion. Please forget about drag drop just need the simple access of function to other js file.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Please read the documentations on how to use the editor if drag drop does not make any sense to you.

    The name of the game object is of no interest.
    The type of component needs to be known to access functionality on it.
    Give the 2d platform tutorial an indepth go.
     
  7. Smallcode

    Smallcode

    Joined:
    Apr 6, 2009
    Posts:
    47
    It is also possible to access a script, or a variable in a script using its name. The script manual in Unity has some useful code-snippets to do this.

    I've used something like this, with some success:

    Code (csharp):
    1. var go = GameObject.Find("ObjectName");            go.GetComponent(ScriptName).VariableInScriptName=1;
    This snippet finds an object in the same scene called "ObjectName", and then finds a Component (in this case, a script) within that object called "ScriptName". It then sets the value of a variable in that script, in this case called "VariableInScriptName, to 1.

    I hope that helps! I know that GetComponent gave me a headache when I tried using it first!
     
  8. yogeez

    yogeez

    Joined:
    Mar 4, 2009
    Posts:
    129
    :) yaa you are right, thanks smallcode, i just wanted to know the straight rule thanks a lot. And also thanks to dreamora too.