Search Unity

Accessing other scripts functions / values

Discussion in 'Scripting' started by Martin-Schultz, Dec 4, 2007.

  1. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Hi,

    I created 2 scripts "A" and "B", each one attached to different gameobjects. Scripts A has now a function like "test()" and I would like to call this "test" function from the script B. Now my question: What is the preferred way to do this?

    Can I access from B the A.test() function by doing something like this?

    getComponent("Name of A gameobject").test();

    I'm having a bit of problems understanding how to call a script function. What I understood so far is that a script needs to be attached to a game object before I can execute an function of that script, is that right?

    Thanks,
    Martin
     
  2. NYGhost

    NYGhost

    Joined:
    Sep 20, 2007
    Posts:
    55
    GetComponent is a member of GameObject so you have to get a reference to the script like this
    Code (csharp):
    1.  
    2. var scriptObject: Object = theGameObject.GetComponent("myScript");
    3. scriptObject.Test();
    4.  
    you may need GameObject.Find("name"); if you dont have a reference to the GameObject.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep. You can use an empty game object for this purpose if you want, if you just need a script on something. Typically something like that is done for game managers. Although you can use any object that's always in the scene, like the main camera or something...doesn't really matter. Probably the most straightforward, assuming that the script called "ScriptName" is attached to the object called "SomeObject":

    Code (csharp):
    1. var someScript : ScriptName = GameObject.Find("SomeObject").GetComponent(ScriptName);
    2. someScript.Test();
    You can also assign the object's script manually. If you write:

    Code (csharp):
    1. var someScript : ScriptName;
    2.  
    3. function Start() {
    4.     someScript.Test();
    5. }
    Then you drag the object with the script "ScriptName" attached to it onto the slot "someScript" in the inspector.

    Also, you can set a static variable in SomeScript to itself so you don't have to get a reference at all:

    Code (csharp):
    1. static var use : ScriptName;
    2.  
    3. function Awake() {
    4.     use = this;
    5. }
    6.  
    7. function Test() {
    8.     // Stuff here....
    9. }
    This way, anytime you want to refer to a function in ScriptName, just do "ScriptName.use", like so:

    Code (csharp):
    1. ScriptName.use.Test();
    Just make sure that "use" is defined first. Which is to say, never refer to "use" in any other Awake function.

    --Eric
     
  4. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Awesome, thanks for the explanations. That clears up a lot of questions I had. Thanks for that!

    (I'm sure I'll be back soon with new questions... :))