Generic "Static" functions

Hey guys,

Is there a way to create a script full of utility functions that are referenced by other scripts? I’d like to make some generic functions. I’m not sure how to do this without have to apply or reference the script in every object that uses it.

I guess what I’m looking for is something like C++ static class functions. You don’t have to instance the class to access the function. Is there something like this in Unity?

Thanks,
Rexx

I think I may be on to this. My script is in C#. Do you have to tell Unity to compile the script? It doesn’t look like unity is seeing it.

Got it! I had to put the script in my standard assets list.

Since I am currently learning C# from the very basics, I got a similar problem but wasn’t be able to get it to work so far :frowning: I would like to have a class that contains useful re-usable functions and can be called from within any other script.

This is what I do:

Script “UtilityStuff .cs” (contains utility functions -not attached to a game object):

using UnityEngine;
using System.Collections;

public static class UtilityStuff : object
    {

    
    public static string MyUtilityFunction(string txt)
        {
        return txt;
        }
    }

And this is my script that needs to call the utility function (this script is attached to an empty object):

Script “Player.cs”

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
    {

    void Start()
        {
        Debug.Log ( UtilityStuff.MyUtilityFunction("HI THERE!") );
        }

    void Update()
        {

        }
    }

However, this results in an error message:
“Player.cs error: The name ‘MyUtilityFunction’ does not exist in the current context” :shock:

Moving the “UtilityStuff” script into the “Standard Assets” folder didn’t help -same problem :frowning:

I just tried with javascript, it seems to work. Simply set static before function in a script.

Exemple, in a javascript file called TestPurpose :

static function ShouldWork  () {
	Debug.Log ("working");
}

From any script, you can use TestPurpose.ShouldWork (). It will show “working” in the console. TestPurpose don’t require to be on a gameObject.

Yes, this is quite easy in JS and actually I did exactly this before I decided to swap my project from JS to C#. But how could this be done in C#? :frowning:

Ok, I basically got it to work now, but I still get a warning:

“Player.cs”: warning: The type “UtilityStuff” conflicts with the imported type “UtilityStuff”. Ignoring the imported type definition"

What does this mean :?:

Script “Player.cs” (attached to an empty game object):

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
    {

    // Use this for initialization
    void Start()
        {
        Debug.Log( "TEST: " + UtilityStuff.MyUtilityFunction("TEST!") );
        }

    }

Script “UtilityStuff.cs” (not attached to a game object, placed in folder “Standard Assets”):

using UnityEngine;
using System.Collections;

public static class UtilityStuff
    {

     public static string MyUtilityFunction(string txt)
        {
        return txt;
        }
    }

Do you happen to have 2 classes (in two files) with the name UtilityStuff? I did a search on that error and that was a possible reason. I also noticed that in 2 of your posts, one UtilityStuff inherited from object, one didn’t.

Also, the folder placement of scripts doesn’t really matter, if I’m not mistaken.

I got it to work now. I am not sure what was wrong, but this is how I am doing it now:

using UnityEngine;
using System.Collections;

public class UtilityFunctions
    {

    public static string MyUtilityFunction(string txt)
        {
        return txt;
        }
     }

So the utility class is inherited from nothing and the methods in there are static. And yes, it does not seem to matter where the utility class is placed in the projects pane.

one line version;

public static string MyUtilityFunction(this string txt) => txt;

2 Likes