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

Using static functions, im confused

Discussion in 'Scripting' started by Anim, Sep 2, 2009.

  1. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
    Hiya

    I have a main script in my scene that handles most of my game code. This script has a few functions in it that can be called from other gameObjects so I had to make them static functions.

    I want some of these static functions to call normal functions within the same script but they always complain with things like "Assets/Game Scripts/MainSetup.js(156,17): BCE0020: An instance of type 'MainSetup' is required to access non static member 'PlayAudioClip'."

    Why is this? Why cant a static function also use functions that arn't static from within its own script?

    Thanks for any help
    Geoff
     
  2. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
    Here is some more info.

    I am trying to call the following function:
    Code (csharp):
    1.  
    2.  
    3. MainSetup.js
    4. ============
    5.  
    6. var explodeClip : AudioClip;
    7.  
    8.  
    9. function PlayAudioClip (clip : AudioClip, position : Vector3, volume : float)
    10. {
    11.     var go = new GameObject ("One shot audio");
    12.     go.transform.position = position;
    13.     var source : AudioSource = go.AddComponent (AudioSource);
    14.     source.clip = clip;
    15.     source.volume = volume;
    16.     source.Play ();
    17.     Destroy (go, clip.length);
    18.     return source;
    19. }
    ....and later on in the same script i have a static function like...

    Code (csharp):
    1. static function killit(name : GameObject)  {
    2. {
    3. ...
    4. PlayAudioClip(explodeClip, name.transform.position, 1);
    5. }
    This causes the error
    Assets/Game Scripts/MainSetup.js(156,17): BCE0020: An instance of type 'MainSetup' is required to access non static member 'PlayAudioClip'.


    I believe I need to pass in some object type as in SomeObjectTypehere.PlayAudioClip(.....

    But how?

    Any help would be greatly appreciated.

    Thanks
    Geoff
     
  3. Anim

    Anim

    Joined:
    Aug 11, 2008
    Posts:
    289
    Solved

    I was using static functions where I should have used Public functions (thanks to . Thats my bad as the docs said for global vars use static, i just assumed that was the same for functions (which worked too but i was using them in the wrong context).

    So, leaving the vars as static, making the function public and calling the actual function from another script in the form...

    var temp = GameObject.Find("someGameObject");
    temp.GetComponent(myScriptName).doSomething();


    Thanks to Duckets, NCarter and Sophie_H for their help.
     
  4. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Edit: D'oh ;)

    static doesn't mean what you think it means:
    http://javaforyou.wordpress.com/2008/06/24/static-members-static-methods-and-static-variables/
    (it's about Java, but static in Java means the same as Unity .js).

    What you want is the opposite of static functions: instance functions (any function that isn't static). To call instance functions, you need a reference to an instance of the class. In Unity, these references are usually obtained by using GetComponent:
    http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html