Well, I might be missing something, but this is how I understand the issue; everything below is in C# and strongly typed.
GO has a list of attached components, containing various items including MonoBehavior and derived scripts from it.
To get a script of a certain class, I have to do
ActorBase actor = this.gameObject.GetComponent(typeof(ActorBase)) as ActorBase;
This works fine but has double penalty because of GetComponent() and typeof().
Now, as Eric suggested, you can cache bunch of references and I use it in all our games. It works fine if you cache it for the same GO for re-usage later. Things get complicated during trigger and collision checks.
To get a trigger check, I override a method in a MonoBehaviour script:
class Player : MonoBehaviour
{
...
void OnTriggerEnter(Collider other)
{
GameObject whoCollided = other.gameObject;
ActorBase actor = whoCollided.GetComponent(typeof(ActorBase)) as ActorBase;
if (actor != null)
{
switch(actor.Kind)
{
case ItemType.Bullet:
...
}
}
}
...
}
Things are getting interesting, aren’t they? Now I have to use GetComponent() and typeof() to extract a base class ActorBase to find out what kind of object collided with the given one. I could use Tag property, but it will force GC kick in quite soon and does not worth it, plus string comparing is also slow. I can cache typeof(ActorBase), but it still not a perfect solution.
If we had a custom native int field in GameObject, things will be easier and faster, so we can avoid comparing strings with GameObject.Tag:
class Player : MonoBehaviour
{
...
void OnTriggerEnter (Collider other)
{
GameObject whoCollided = other.gameObject;
switch(whoCollided.User_Int)
{
case ItemType.Bullet:
...
}
}
...
}
Not using GetComponent() and typeof() will save about 5-10ms, which is a huge amount of time and gives about 10-30fps increase.
Note: dont think that while you can write less code in JavaScript, the final compiled code will be smaller - no, the JS will add typeof() behind the scenes.
I believe that single integer field will solve a lot of trouble for iPhone.
@Eric: beware of the circle references in your example, you need to set all the cached variables to “null” when that class is destroyed/disposed, otherwise you’ll be keeping the GO from finalizing by GC.