Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Servail · Jan 30, 2018 at 06:54 PM · gameobjecttransformgetcomponentthis

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Jan 30, 2018 at 07:01 PM 2
Share

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.

avatar image MaxGuernseyIII TreyH · Jan 30, 2018 at 07:47 PM 1
Share

This is the correct answer disguised as a comment.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Servail · Feb 01, 2018 at 06:42 AM

Fixed easily! gameObject = transform.gameObject;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Feb 01, 2018 at 12:22 PM 0
Share

Those are literally the same thing. This isn't an answer, it's a basic misunderstanding of how components, scripts, and gameobjects are related.

avatar image Servail TreyH · Feb 02, 2018 at 12:43 PM 0
Share

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).

avatar image
4

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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
 
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Servail · Feb 01, 2018 at 01:36 AM 0
Share

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!

avatar image Commoble Servail · Feb 01, 2018 at 06:09 AM 1
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Operator '==' is ambiguous on operands of type 'Vector2' and 'Vector3' 1 Answer

Assets/CArSmoothFollow.cs(40,68): error CS0246: The type or namespace name `ExampleGame_Game' could not be found. 1 Answer

access object from another 2 Answers

NullException Error while instantiating gameobjects 0 Answers

Why does destroying a gameObject influence its former children? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges