Low-cost alternatives to Monobehavior?

I have a script that holds public variables for other scripts to access. I never thought I’d need such a thing, but here we are.

So, the class is literally empty except for a public variable (and a ‘using’ namespace to let me have that variable) so really I don’t need the class to extend from monobehavior, do I? The script isn’t using anything I get from monobehavior except for the ability to attach it to a gameObject.

What other options are there for classes I can extend from? Just to shave a little bit of overhead off of my game…

Would this be something you could use a scriptable object for?

No harm in making one that just holds one value, akin to that very popular Unite video on them:

[CreateAssetMenu(menuName = "Variables/Float Value")]
public class FloatValue : ScriptableObject
{
    [SerializeField]
    private float floatValue;

    public float Value { get { return floatValue; } set { floatValue = value; } }
}

I have one of these for other basic values. Int, Boolean and Vector3.

Aye, and that is an excellent use… I have a kind that stores a string yet lets you effectively store anything in it.

I call it … Datasacks!

8687265--1171653--20180724_datasacks_overview.png
Mostly for UI but can be used for any sort of data really.

Datasacks is presently hosted at these locations:

https://bitbucket.org/kurtdekker/datasacks

Dang, I still need it to attach to a gameObject though, I got a bunch of errors when I tried to change it to ScriptableObject.
The game object exists in the scene and also has a collider on it; I’m using Physics.OverlapSphere on another object so I can get a reference to a path in my scene as suggested in this thread.

Is there another level of class that can still attach to a game object?

I’m tellin’ ya Mars, scriptable objects.

You make one, it lies as a file in your project.

You drag that file (let’s call it “Score”) into any slot in any script that needs access to it.

There’s tons of videos to check out, but start with something like this:

https://www.youtube.com/watch?v=aPXvoWVabPY

1 Like

Yes, but I’m doing this to set up objects I DON’T have in my scene; I’m instantiating some enemies at run-time and I need them to see a reference to a path that they will follow.
My solution was to put a trigger collider near where they spawn, and have the enemy use overlapSphere to find this, and then grab the path reference from them.

Well we didn’t know that at the beginning of the thread.

Scriptable Objects exist at the asset level, and they can’t serialise reference to scene-level objects (but can store nonserialised references to them at run time).

For storing references to stuff at the scene level, yes Monobehaviour’s are what you need.

Also I wouldn’t worry too much about having small monobehaviours that hold small amounts of data. If they aren’t running anything in update then it’s just memory waiting to be accessed. It is a component based game-engine after all. Used it for what it’s good for!

Why nost just have a static class? I use them for constants and such stuff. Some will say “globals are bad” but when you know their constraints what could go wrong? :wink: Seriously. Don’t overcomplicate things. When there is a quick and easy solution to a problem use it. When you encounter problems/issues you can change it.

1 Like