#Components

1 messages · Page 1 of 1 (latest)

ocean chasm
#

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.

open citrus
#

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

ocean chasm
#

Link some code to show this 'way' :)

paper socket
#

You can call GetComponent from GetChild. No need to add .gameObject . . .

open citrus
#

gotcha, otherwise, is this correct? It feels very easy to spaghetti my code so I am trying to keep it clean

paper socket
#

Also if only one of the objects has the component you need, using GetComponentInChildren is fine as it can only find the one . . .

ocean chasm
#

Look out for
GetComponent
GetComponent**s**

ocean chasm
open citrus
#

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?
ocean chasm
#

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

open citrus
#

Serialized field meaning ```cs
class Slider : Monobehavior{
public Transform myChildItemsTransform;
}

?
ocean chasm
#

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

open citrus
#

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?

ocean chasm
#

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.

open citrus
#

alright gotcha. I know what a serialized field is in general, so I will go figure out how to impliment it in this context

ocean chasm
#

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.