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
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!
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!!