Hi, I would like to associate some user data with a GameObject, much like a void pointer so I can access it by
GameObject gameObject;
Someclass s = (Someclass)gameObject.userData;
Hi, I would like to associate some user data with a GameObject, much like a void pointer so I can access it by
GameObject gameObject;
Someclass s = (Someclass)gameObject.userData;
You are more likely to get a response if you ask a question, but in this case I know what you are “asking.”
Without knowing the nature of Someclass, I have two suggestions.
You can extend the GameObject class, and add a custom property, and use that.
If Someclass is a component/script, you can use
gameObject.AddComponent(Someclass);
Someclass s = (Someclass) gameObject.GetComponent<Someclass>()
Typically you would use a component for this (see sccrstud92’s second example).
I’m not so sure about that, even if you can, there is no way of using it as all GameObjects use their base class.
This is the most viable case.
Sorry, I’m still new to Unity. I didn’t know you couldn’t extend GameObject like that and still use it like a GameObject. It’s what I would have done on a non-Unity application, so that’s why I suggested it.
I’m trying to avoid using GetComponent because it’s known to be slow. I’m hoping to make it a direct access like gameobject.transform, without the performance hit. Because it is a common component, I like to reduce memory instead of caching it per script.
Use GetComponent once at the beginning of the program, and store the reference into a property. Then, every time you need to access the component, just use the property instead of calling GetComponent.
This is what I’m trying to avoid. I have a few of these components, and they are common in many of the scripts, so I have to duplicate them in every script component, not to mention wasting memory since each script cache their local copy.
Give us more information on the nature of “Someclass,” and maybe we can give some more suggestions.
Wasting memory? its just a pointer !
Sometimes you need to waste memory to win performance.
There is
So the data map is used by most other scripts, same as partitioning scripts, and few others etc.
I’m expecting few hunderd of objects on screen, with 5 components each, on limited platforms, I would like to avoid memory, and reduce typing. Every startup calling GetComponent also increase loading time unnecessarily.
Limited platforms (iPhone?) can’t deal with that many on-screen objects in the first place, because of not having enough CPU + GPU power.
A desktop computer can do a million GetComponent calls in a fraction of a second. It’s “slow” only in comparison to direct access, which is why you would normally only bother to cache a GetComponent call if you’re doing it repeatedly. Even on an iPhone, a few hundred GetComponent calls isn’t going to take a perceivable amount of time. According to System.GC.GetTotalMemory, 1000 GetComponent references uses about 4KB…not exactly an amount to worry about even on limited platforms. It doesn’t store a copy of the script, if that’s what you’re thinking. As AzulValium said, it’s just a pointer.
–Eric