Attaching Additional Data to a GameObject without Update behavior

I've got a scene with 10,000 GameObjects in it and I need to associate some additional data with each object. I can accomplish this using a script with a class that derives from MonoBehaviour, but if I do this, my framerate drops because the engine calls Update() on every GameObject in every frame. I don't intend to implement Update(), but it's killing my framerate.

I tried making my class derive from Component, but then I cannot attach it to my GameObject prefab because it must derive from MonoBehaviour.

Is there a better way to attach a class to a GameObject to get more data without requiring the Update() logic to run every frame?

1 Answer

1

If you have an Update function in your script, it'll run. If you don't, it'll skip it and you'll get some cpu cycles freed up

What you could alternatively do is create a Dictionary/HashTable with GameObject as key and YourCustomClass as value, so you can set and get data via that

I didn't realize it was as simple as deleting the Update() function. In my mind, it was simply an override to a virtual function that would get called every frame regardless of the implementation. The solution really is as simple as deleting the Update() function from the script. Thanks Mike!

If it were a virtual function, you'd have to use the override keyword. The way it actually works is that it stores a pointer to your function when the script is instantiated, and calls that

Clever! I wondered why I didn't have to use the override keyword. Made that mistake a few times trying to actually implement some overrides.