Home > Net >  Unity how to set a prefab property trought an array
Unity how to set a prefab property trought an array

Time:10-22

Could you please help me to write the right syntax? I am stuck with the following code:

GameObject cube = (GameObject)Instantiate(cube_prefab, new Vector3(x, y, 0), Quaternion.identity, transform);

cube.GetComponentInChildren<TextMeshPro>.text = "test" **// WORKS FINE**

Take into account that inside my prefab i have more TextMeshPro, so my question is: how can I get to the second object if i can't access trought an array ? sounds weird for me

cube.GetChild(0)<TextMeshPro>.text = "test" // DOESN'T WORK
cube.GetChild(0).GetComponent<TextMeshPro>.text = "test"; // DOESN'T WORK 
cube.GetComponentInChildren<TextMeshPro>.GetChild(0).text = "test"; // DOESN'T WORK
cube.GetComponentInChildren<TextMeshPro>[0].text = "test" // DOESN'T WORK 

Thanks in advance

CodePudding user response:

The GetChild(int) method is a method of transform. In your example, you're calling GetChild(0) from the GameObject instead. So the fix in your example would use the transform property of your "cube", find the child, then get the component of the child item:

GameObject cube = (GameObject)Instantiate(cube_prefab, new Vector3(x, y, 0), Quaternion.identity, transform);

cube.GetComponentInChildren<TextMeshPro>.text = "test" **// WORKS FINE**

cube.transform.GetChild(0).GetComponent<TextMeshPro>.text = "test"

Here's the Unity docs for GetChild(int).

If you wanted to iterate over the next level of children for your game object, you could do this:

var t = cube.transform;
var childCount = t.childCount;
for ( int i = 0; i < childCount; i   )
{
    if ( t.GetChild(i).TryGetComponent<TextMeshPro> ( out var tmp ) )
    {
        tmp.text = $"TextMeshPro found on child {i}";
    }
}

Be aware, that this will only iterate over the direct children of "cube", not the children of those children. You would have to check the child count of each child to check further down the family tree.

CodePudding user response:

Sorry if I sound like I'm preaching, but it seems you just try to guess what the syntax is, without knowing what it each thing really does, and it won't do you any good. You should look into methods with generics (especially how to call them) and try to write code meaningfully instead of throwing brackets here and there, and expect things to happen.

With that out of the way, let's look at your issue.

  1. The name 'GetChild' does not exist in the current context, because your cube is of GameObject type, which indeed doesn't have a GetChild method. The component that does have it is Transform. You should call it like: cube.transform.GetChild(0)...

  2. If you fix that, the next issue is that GetChild method doesn't use generics (<Type>) and even if it did, first you provide should provide a type in angle brackets, and then write regular brackets () to indicate method call. What you probably wanted is to first get child, and then to get a component in it: cube.transform.GetChild(0).GetComponent<TextMeshPro>().text = "test";

  3. In cube.GetComponentInChildren<TextMeshPro>.GetChild(0).text you miss brackets: cube.GetComponentInChildren<TextMeshPro>().GetChild(0).text. Morover, the TextMeshPro doesn't have a GetChild method, you must have confused ordering of your method calls.

  4. In cube.GetComponentInChildren<TextMeshPro>[0] you try to use syntax as if you were using arrays, while GetComponentInChildren is just a method, not a property that is an array.

To answer your question shortly: use yourGameObject.transform.GetChild(childIndex).GetComponent<TextMeshPro>().text transform.GetChild() to navigate down to your desired gameObject and only then call GetComponent (remember brackets!) to get to your text property.

  • Related