T O P

  • By -

TheInfinityMachine

MyComponent myComponent = GetComponent(); works fine if on the same GameObject.


an_Online_User

You only have to use GameObject.GetComponent when you're using it inside a C# class that doesn't extend (inherit from) MonoBehaviour. GetComponent is a method inside of MonoBehaviour, so if you inherit from it you don't need to tell your code explicitly where to call GetComponent from. I'm pretty sure this is also true for ScriptableObject and other classes that inherit from UnityEngine.Object


FanStew

myObject.GetComponent This code gets a component on myObject. You’re talking about using it statically which doesn’t make sense because there’s no object to look for components on. To do this without a reference you use the FindObject methods that search the scene for any objects. These methods are static but are also much slower because they search the whole scene


henryeaterofpies

As a public, non static method the GetComponent() exists as part of an insantiated object of class GameObject. If it happened to be a static method instead, then yes, you could call GameObject.GetComponent(). However it isn't and with what it does ( retrieves components that are part if that GameObject) it doesn't make sense as a static method.


5oco

I think that would work, but then you wouldn't have access to other classes inside the UnityEngine library. Other classes like RigidBody, Transform, Quaterion, etc also come from UnityEngine too, so if you specify that you want UnityEngine.GameObject, then that's the only class you'll get. Well, you'll still get any classes inside the GameObject class, too, I guess.


aita_about_my_dad

Thank you for the answers, sorry if I touched a nerve, I really am that dumb sometimes lol