How can i use the method of a non monobehaviour class in my normal scripts.

I’m trying to call a method from an utility class, but can’t get to solve all the errors notified by Unity.

My objective right now is to select gameobjects by clicking on them (working fine now) thanks to apply a code to each objects i want to be able to select and an use of raycast on the camera.

Now i creataed an utility class that i want to call for retrieve a table of all the selected game objects.

Utility class code :

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Utilitaires
{
    // Use this for initialization
    public GameObject[] FindSelectedObjects()
    {
        GameObject[] gameObjects = (GameObject[]) MonoBehaviour.FindObjectsOfType(typeof(GameObject));
        List<GameObject> selectedGameObjects = new List<GameObject>();
        GameObject[] bl = null;
        for (int i = 0; i < gameObjects.Length; i++)
        {
            variables o = gameObjects*.GetComponent("variables") as variables;*

if (o != null)
{
if (o.isSelected())
{
selectedGameObjects.Add(gameObjects*);*
}
}
}

if (selectedGameObjects.ToArray().Length == 0) { return (GameObject[])bl.Clone(); } else { return (GameObject[])selectedGameObjects.ToArray().Clone(); }
}
}
My code accessing to the utility class :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Blabla : MonoBehaviour {

public GameObject[] selectedObjects;
public Utilitaires utilitaire;

void Update()
{
selectedObjects = utilitaire.FindSelectedObjects();
}
}
The only error notified by unity right now is at selectedObjects = utilitaire.FindSelectedObjects();
telling :
> NullReferenceException: Object
> reference not set to an instance of an
> object Blabla.Update () (at
> Assets/Blabla.cs:12)
does anyone have an idea of what’s going on ?

Utility classes usually contains just static functions. Usually you don’t need / want an instance of such a class (note i said usually, there are always situations where it’s different :wink: ).

Almost all Unity provided functions are also static functions in Utility classes. You never create an instance of GUI, Mathf, Input, Network, Physics, …

So just declare it like this:

public class Utilitaires
{
    public static GameObject[] FindSelectedObjects()
    {
        //...

And use it like this:

Utilitaires.FindSelectedObjects()

If the class should only contain static stuff, make it a static class so you can’t forget to make all your functions static:

public static class Utilitaires
{
    // Only static members allowed.
}

Well you need to create an instance of your utility class:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Blabla : MonoBehaviour {

    public GameObject[] selectedObjects;
    public Utilitaires utilitaire  = new Utilitaires();

    void Update()
    {
        selectedObjects = utilitaire.FindSelectedObjects();
    }
}

Actually @Josmun the best way to do this is not using the methods described above. You should use the ScriptableObject class for the utility classes, such as beans or libraries. If they are beans, you should also use Serializable to store and manage the data.

For your example, you should use:

 public class MyLibrary : ScriptableObject {
     
     public GameObject[] FindSelectedObjects(){
//TODO your logic
}

 }

Then if you need to use that function, you should create an instance of your class NOT USING the “new” keyword, but using the CreateInstance function from the ScriptableObject class, and use this instance to access it:

MyLibrary libraryObject = ScriptableObject.CreateInstance<MyLibrary>();
GameObject[] selectedObjects= libraryObject.FindSelectedObjects();

For more information about ScriptableObjects, please refer to:

Best Solution : Unity - Manual: Running Editor Script Code on Launch