Using static functions, im confused

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

Here is some more info.

I am trying to call the following function:

MainSetup.js
============

var explodeClip : AudioClip;


function PlayAudioClip (clip : AudioClip, position : Vector3, volume : float)
{
	var go = new GameObject ("One shot audio");
	go.transform.position = position;
	var source : AudioSource = go.AddComponent (AudioSource);
	source.clip = clip;
	source.volume = volume;
	source.Play ();
	Destroy (go, clip.length);
	return source;
}

…and later on in the same script i have a static function like…

static function killit(name : GameObject)  {
{
...
PlayAudioClip(explodeClip, name.transform.position, 1);
}

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

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.

Edit: D’oh :wink:

static doesn’t mean what you think it means:

(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: