- Home /
Is there a "Lightweight Monobehaviour"?
Hey there,
Problem: I have an Transform array 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 choise for such a problem?
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.
Thanks in advance!
I don't know what the overhead is for a component, but I don't think it can add much to your app (compared to say one bitmap) unless you have a huge number of chairs. If the only place the boolean is accessed is from the array, then create an array of structs containing one transform and one boolean.
From memory of the results of some tests I did a while ago, if you do create a huge number of $$anonymous$$onoB's and don't need Start() or Update(), make sure you delete them from the template file. Having empty function definitions seemed to add a slight overhead for each one, but otherwise I agree with @robertbu - I think you might be unnecessarily pre-optimising here.
Answer by Owen-Reynolds · Aug 08, 2014 at 07:14 PM
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, OnCollisionEnters .... 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[i].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.
Thank you! So I will end up with every chair having a script that stores it's current status. When the overall opinion is that this will not cause noticeable overhead, then this is a solid solution. Thanks again.
Your answer
Follow this Question
Related Questions
Using a non-monobehaviour class file 1 Answer
Script as a variable being assigned later in script??? 3 Answers
Reading data from one script to another 0 Answers
How to save data on iOS? 1 Answer