Hi there,
I have a few questions on components, and what they are. For example, I presume, any setting (component) I can select, to attach to an object, from the drop down menu, Component is well, a component!
Now, having said that tho, just comparing all the possible components I have access to, if I compare that to the list given in the Script Reference (Unity - Scripting API: Component) It just simply does not cover them all. So I am wondering, why is this?
That’s not meant to be a list of all components you have access to, it’s just a list of variables for the Component class. For the most part those are the components you probably would use more often, so you can write “transform.position” instead of “GetComponent(Transform).position”.
Yes, but you don’t need to store a viable for that when you can directly do transform.scale = new Vector3()…
You need to use pointers when you are accessing components from another game object only.
It’s also a good idea to store the Transform in a variable instead of making reference to gameObject.transform all the time since it is doing the GetComponent call behind the scenes.
Right,
so on script declaration, declare the var itself at top. This var, is one that may be called quite a bit through the scripts life. This var could also be in reference to the owner of the script or any outside object.
GetComponent used to be slower than it is now. That’s why there’s the notion still floating around that “GetComponent is slow”, but it’s not really true. A modern CPU can do at least 10 million GetComponent calls per second, though it does depend on how many components you have on an object and which of those components you’re getting. If you have, say, a few dozen objects doing “GetComponent(Transform).Translate” every frame, it will make essentially zero difference whether you cache the GetComponent call or not. It’s still technically more efficient to cache GetComponent calls, but don’t obsess over it too much.
Do cache them if you’re doing say bullets or where there will be an awful lot of them. Don’t bother if it’s just the main player or things here and there.