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?
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(…
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.
(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: