- Home /
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[i].GetComponent("variables") as variables;
if (o != null)
{
if (o.isSelected())
{
selectedGameObjects.Add(gameObjects[i]);
}
}
}
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 ?
Answer by Bunny83 · May 31, 2012 at 11:55 AM
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 ;) ).
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.
}
I learned something about static, thanks. You just made me upgrade one level in programmation. Thanks !
Answer by whydoidoit · May 31, 2012 at 10:52 AM
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();
}
}
Thanks a lot, that did it completely.
Sorry i'm more used to Javascript usually.
I have been creating new instances of my non-monobehaviour classes and using different constructors in those classes. Is this ok. Do I understand the rules correctly :
You may not use constructors in classes that inherit from monobehaviour (there you should use Start() and Awake()) . But in classes that do not inherit from monobehaviour you can use constructors of those classes by creating new instances / objects of them.
?? is this correct ??
Answer by David_Munoz · Nov 06, 2015 at 01:45 PM
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:
http://unity3d.com/es/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects
[1]: http://unity3d.com/es/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects