1 When I get a reference to a game object, how does it keep track of its components? Does every game object keep a list (reference to array-like structure) that keeps hold of the references to the components?
2 Suppose I have a reference to some game object that I’ve found - maybe I’ve done a raycast, hit some object A, and I would like to do something with it - if it has some “HitEffects” component, I’d like to call some particular method of this component. Generally, one option is to just call GetComponent.
This seems somewhat inefficient though - wouldn’t it be useful to have a direct, customizable reference to a specific component on the game object? For each game object we could just have a reference to one specific game component on this given game object, let’s call it “DefaultComponent”.
This way, if someone wants to, instead of having to call some generic version of _gameObject.GetComponent, they could just call _gameObject.DefaultComponent directly, pass in the proper arguments and have the object do what they want, or returning the reference to the component.
This isn’t a huge difference to using GetComponent, because in a lot of cases, the DefaultComponent will still have to do some sort of comparison to find what you’re looking for, but in a lot of cases this could be made far more efficient than a GetComponent that might have to go through 10+ components. I’m writing it out as an example of the direction in which I’m thinking.
For even more flexibility, why not just keep the components in some sort of ordered list? That way, I could just directly access say the “first” component of the game object, and I’d know it’s the one I want to use. Might be a bit messy to keep track of, but it could still be useful. Isn’t it also done with transform already anyway? I’d expect that a gameobject doesn’t actually search through it’s components to find the transform, but I wouldn’t exactly be surprised if it did.
//
All of this are just details, but I’m surprised something like this isn’t really possible. I know that if a raycast hits an object with “Enemy” tag, I’ll end up calling some method in the “HitEffects” component on the object, and this is what will always happen in the specific raycast. But despite the simple goal, I have to do a GetComponent.