#Components
1 messages · Page 1 of 1 (latest)
We'll thread this.
So from reading the page about MonoBehaviour (best bookmark)
You may have learned that you'll never have to get GameObject (container) or the Transform component.
Alright, I appreciate the help. I am not new to coding, but new to Unity. There seems to be a million ways to do this, and my way works. So, im a bit confused abt the "correct" way
You can call GetComponent from GetChild. No need to add .gameObject . . .
gotcha, otherwise, is this correct? It feels very easy to spaghetti my code so I am trying to keep it clean
Also if only one of the objects has the component you need, using GetComponentInChildren is fine as it can only find the one . . .
Look out for
GetComponent
GetComponent**s**
I'm kinda waiting for a reply or a code segment.
alright awesome makes sense now
Oh, the example did not get put into the thread. so ```cs
//get the text field of my child
transform.GetChild(0).GetComponent<TextMesh>();
Now i know that if i only have 1 child with a TextMesh, but just in general, this is how you access and interface with children?
The best way is to use a serialized field.
Then you can get the component directly in that field, without using the expensive GetComponent function
Serialized field meaning ```cs
class Slider : Monobehavior{
public Transform myChildItemsTransform;
}
?
[SerializeField] private (when public isn't necessary (both are serialized))
When a field isn't possible, because the object is spawned at runtime, GetComponent can be used, but should be avoided inside Update().
To avoid GetComponent during gameplay, one can store the reference to the necessary components, whenever the object is instantiated, and keep it in a singleton, so other scripts can access it directly from there. (not a priority for beginner prototyping)
Alright, so i understand what you mean by singleton. So, do you mean to say that the child should actually expose their transform in some way that the parent can read it?
If a parent needs any component from its children, and the children are not spawned after the parent, then you should use a serialized field, to avoid using GetComponent, which is considered an expensive operation.
alright gotcha. I know what a serialized field is in general, so I will go figure out how to impliment it in this context
Last thing
GameObject.Find() is not valid in a build, and should be used exclusively inside void Reset(), which is an editor-only function that is stripped from build.
It can be used to set default values for your serialized fields.
[SerializeField] private BoxCollider2D childCollider;
private void Reset()
{
childCollider = GameObject.Find("Alice").GetComponent<BoxCollider2D>();
childCollider = Transform.Find("Alice").gameObject.GetComponent<BoxCollider2D>();
}
and from BoxCollider2D you have access to GameObject and Transform by
childCollider.transform
childCollider.gameObject
3 in 1
Transform.Find() only searches children
If there are no other questions, you may close the thread.