Hey there,
Problem:
I have an Transform []
of chairs, each one can be empty or occupied.
I think extending Monobehaviour
would be a little overhead.
So is there a way to attach a script to a GameObject
that is not extending Monobehaviour
(or ScriptableObject
).
And is ScriptableObject
a choice for such a problem?
Thanks in advance!
Edit:
I just noticed that when I add a ScriptableObject
-Script to one chair, you are not allowed to add it to another chair, so this way it would not work either.
I don’t think extending MonoB
is that big a deal. I assume you mean you need a small data script:
class chairData : MonoBehaivoir {
public bool Occupied;
}
I think the engine merely adds all of your Updates, OnCollisionEnter
s … to a list. I don’t think
that every frame it loops through every chair object, looking for a nonexistant Update
.
But if you really want it lightweight, just don’t put the class on the object. If you have 20 chairs, store the list of 20 occupied vars in an array, elsewhere:
// some "master" script, which can also hold chair data:
class playerScript : MonoB
{
class chairData {
public Transform chairT;
public bool Occupied;
}
chairData_t[] Chairs;
}
At start, fill the Chairs
array with a pointer to each actual chair. The drawback is there’s no good way to go from a real chair back to this. Like, if someone clicks on a chair, have to loop through the array, checking Chairs.chairT==clickedOnChair
. But that’s not really a problem.
I’ve found I usually want to add OnCollision
or some other MonoBehaiv
function, or set Inspector vars by hand. So I usually end up rewriting these as MonoB
scripts, anyway.
Disable the MonoBehaviour
. I did come across a similar situation. I had 100.000 objects in the scene every one of them had a MonoBehaviour
with absolutely nothing in it. And when i run the game with disabling it i gained over 20+ fps. I think unity is registering update functions based on the state of the script.