this.GetComponent() won't return gameObject? How to get it without using this.gameObject?
I'm currently writing workaround over standard MonoBehaviour. I have a similar code:
public GameObject gameObject;
public Transform transform;
void OnValidate() {
gameObject = GetComponent<GameObject>();
transform = GetComponent<Transform>();
}
transform works just fine, but gameObject returns none/null! But if I replace gameObject with _gameObject in example, or remove assignment OnValidate and assign in inspector by myself it works! Is it bug or something? UPD: So, how to get current GameObject without using this.gameObject?
GameObjects are not Components and cannot be retrieved with GetComponent(). You can assign it in the inspector because then the inspector knows to look for a GameObject. If you want the GameObject that your script is attached to, then just use this.gameObject
.
This is the correct answer disguised as a comment.
Answer by Servail · Feb 01, 2018 at 06:42 AM
Fixed easily! gameObject = transform.gameObject;
Those are literally the same thing. This isn't an answer, it's a basic misunderstanding of how components, scripts, and gameobjects are related.
Of course. Silly, I have hard times to figure this relation out, so I've posted this question. I'm overriding default gameObject variable of $$anonymous$$onoBehaviour, and doing this instantly, so I can't store gameObject in temp variable. Then how to get it? gameObject = gameObject? But suddenly I realise I'm overwriting this monobehaviour's gameObject only, and transform also have this, and every gameobject has a transform. Yes, maybe that's stupid, but it was my question (if you red it carefully, hehe).
Answer by Commoble · Jan 30, 2018 at 09:06 PM
GameObjects aren't Components. If you need a reference to the GameObject that your script is attached to, just use this.gameObject
. You can also get the transform by using this.transform
, since all components (including your scripts) have access to the transform of the gameobject they are attached to.
Answer by yummy81 · Jan 30, 2018 at 07:43 PM
When I try to execute this code:
System.Type t = typeof(GameObject);
Debug.Log(GetComponent(t));
it gives me the exception:
ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
So, when we look at the inheritance chain, we see that the GameObject does not inherit from either MonoBehaviour or Component or is an interface. But when it comes to Transform, we see that it derives from Component. That's why GetComponent with Transform works. MonoBehaviour also derives from Component and GetComponent with MonoBehaviour works. At the bottom is always System.Object. Even when it comes to the interfaces.
Inheritance chain for GameObject:
UnityEngine.GameObject
UnityEngine.Object
System.Object
Inheritance chain for Transform:
UnityEngine.Transform
UnityEngine.Component
UnityEngine.Object
System.Object
Inheritance chain for MonoBehaviour:
UnityEngine.MonoBehaviour
UnityEngine.Behaviour
UnityEngine.Component
UnityEngine.Object
System.Object
But if I rename my variable to something different than gameObject (i.e. myGameObject) GetComponent() starts working for some reason (or maybe i'm just tired, lol)... Ok then, how do I refer to GameObject without using gameObject (since I want to override it)? I can freely do the assignment in inspector, but i need to do this from code!
Again, if you're trying to get a reference to the GameObject that you've attached your script to, this.gameObject
is a reference to the script's GameObject. Your script already knows where your GameObject is. You can't use GetComponent() to find it because GameObjects aren't components.
Alternatively, if you're not trying to get a reference to the script's GameObject, what ARE you trying to do?