Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Access to prefab child

Discussion in 'Scripting' started by Onilut, Oct 30, 2015.

  1. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    I have made a list of a prefab. this prefab have 2 childs, I want to access to them in the main code to change some parameters in the material component. I did this:
    Code (CSharp):
    1. if (seleccionJuego == 2 || seleccionJuego == 3)
    2.             foreach (GameObject parte in lista)
    3.                 if (parte.GetComponent<Identidad>().activo)
    4.                 {
    5.                     parte.GetComponentInChildren<Transform>().Find("anillo").material.color; //it just doesn't let me to do this
    6.                     //parte.GetComponent<Renderer>().material.color = Color.clear;
    7.                 }
    How can I access to child prefabs properly
     
  2. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    Try this instead on line 5 (not tested):
    Code (CSharp):
    1. foreach(Transform child in transform)
    2. {
    3. if (child.transform.name == "anillo")
    4. child.GetComponent<Renderer>().material.color = Color.clear;
    5. }
    I'm not entirely sure what you're trying to accomplish, but hopefully this helps.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Or use Transform.Find. It's essentially the same thing.
     
  4. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    Hey thxs for the replies and sorry for not aswering before. My main code is in another gameobject (that gameobject works like an empty). and i have a gameobject cube that is public. I attached this cube to that public variable, but i want to access to their childs and change his colors

    edit: I did write this: "cubotemporal.transform.FindChild("anillo").GetComponent<Renderer>().material.color = Color.gray;" but it doesnt work. cubotemporal its a temporal cube that i make, to add to a list.
    Code (CSharp):
    1. GameObject cubotemporal=(GameObject)GameObject.Instantiate(cubo, new Vector3(-4.5f + i,-5.5f + nivelCubo,constanteZ),Quaternion.identity);
    2.             cubotemporal.GetComponent<Identidad>().numero = CuboNumero;
    3.             cubotemporal.transform.name ="Cubo " + CuboNumero.ToString();
    4.             cubotemporal.transform.FindChild("anillo").GetComponent<Renderer>().material.color = Color.gray;
    5.             //cubotemporal.GetComponent<Renderer>().material.color=Color.blue;
    6.             cubotemporal.GetComponent<Identidad>().activo = true;
    7.             cubotemporal.GetComponent<Identidad>().borrar = false;
    8.             lista.Add(cubotemporal);
    9.             CuboNumero++;
    Edit 2: Now i know the problem, when i change the "anillo" material color, it does not change the color I want https://gyazo.com/c7ecb70851d5b049be16a2f4c4a5354d it changes the albedo color, but i want to change the emission color
     
    Last edited: Nov 1, 2015