Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 yoannG · Jan 05, 2017 at 09:31 AM · gameobjectgetcomponentfind

Talking to GameObject and components problem

Hello smart people!

I am trying to attain the legendary skill of mastering how to make my scripts talk with any GameObject and their components wherever they are. To do that, i watched a couple of tutorials like https://www.youtube.com/watch?v=LrkfSqxz4jU, but my brain still seem to resist to smartness :(.

So far, i have understood than in order to do that, i first need my script to find the right gameobject in my scene (if the script is not attached directly to it), and assign it to a variable, with for example:

 myVariable = GameObject.Find ("MyGameObjectName"); 

Then, when i have found this gameobject (and eventually summoned it if it was not in my scene), i find myself at loss to figure out how to call the right component (and inherently, how to call the right sub-element.

For example, i have at the moment a game object for my UI with :

RectTransform, CanvasRenderer,UI Controller (Script),Grid Layout Group (Script)

In order to modify the RectTransform 's Pivot X for example, my logic tells me to add to my script:

 myVariable.GetComponent<RectTransform> ();
 myVariable.RectTransform.Pivot.x = 0.75;

...Which get all red and bad, and i don't understand why. I am also not knowing how i am supposed to call the component GridLayoutGroup. I suppose there is a even dirtier trick in the sense that it is written (script)...

To give you another example that i find confusing, if i would type myVariable.transform.position.x , is it changing the RectTransform, or another hidden transform that i don't know of ? It is confusing because i would think that logically, this should be called instead myVariable.RectTransform.position.x or something.

So the point of all that is: What's the big idea ? What is the core concept that i am missing ?

I am so confused ! :D

Comment
Add comment · Show 1
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 KoenigX3 · Jan 05, 2017 at 03:37 PM 1
Share

The idea of communicating between GameObjects and Components is about getting references, and using them as identifiers.

 public GameObject myObject;

Consider this 'myObject' variable as an identifier. Right now it only tells us, that it has a type of GameObject, and it is public. If you want to refer to a GameObject, you can use the editor, or the GameObject.Find() method, which you have used. Public variables can be seen in the editor, so you can drag any GameObject into this slot.

If the GameObject (which is assigned to myObject) has a Rect Transform Component on it, you can use the GetComponent function to get the reference of the component. But this line does not change the myObject variable - it is still a GameObject. If you want to access the Component, you have to create another variable and assign the RectTransform to it, or use the shortened version:

 public GameObject myObject;
 
     // This is the new RectTransform component variable used in the second solution
     RectTransform myRectTransform;
 
     void Start()
     {
         // The first solution involves no new variable
         myObject.GetComponent<RectTransform>().pivot = new Vector2(0.75F, myObject.GetComponent<RectTransform>().pivot.y);
 
         // This is the second solution with the new variable
         myRectTransform = myObject.GetComponent<RectTransform>();
         myRectTransform.pivot = new Vector2(0.75F, myRectTransform.pivot.y);
     }

I would like to mention that if you use the second solution, your myRectTransform variable can be used again if you want to access it. However, if you do not create a new RectTransform variable and use the first solution, you will have to call myObject.GetComponent again to get the reference of the RectTransform.

Also another little thing: you can not change Vector2 components one by one. You must create a new Vector2 variable, and use that. That's why the first solution is a bit longer: when you are creating the Vector2, you do not want to change the y component of the Vector2 - that is why you have to get the reference of the component again, and ask the pivot Vector's y component.

I hope you could understand this little 'tutorial' thing, my English is not perfect, sorry for that. If you have any questions, feel free to ask!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by yoannG · Jan 05, 2017 at 04:05 PM

Thank you very very much for this detailed, pedagogic and crystal clear answer ! I am especially very greatful to add new words to my vocabulary, like identifier and references. I believe that one of the hardest part of learning programing (and maybe even learning anything), is to get to learn the vocabulary, the logic, the synthax, then maybe how to make poems with it one day =).

Thanks again a million times!!

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

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

103 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

Related Questions

What to use instead of GameObject.Find and GetComponent 2 Answers

Store script from GetComponent ? 1 Answer

Detecting for a variable from multiple potential scripts. 1 Answer

Object reference; sometimes works but sometimes doesn't??? 2 Answers

Make script[] = gameobject[].getcomponet 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