- Home /
On a répondu à la question, la bonne réponse a été acceptée
How can I access specific children of a prefabs I initiate in a code
I'm actually developping my first game on Unity and as I do some codes to initiate a prefabs. The thing is that I still need to have access or at least create a GameObject variable to refer to the childs of the prefabs I previously created.
To shorten what I ask:
I want to create a prefabs during runtime with C# (No need to specify anyway to generate it, I have my own idea) I want to have access to the prefabs children I want to be able to modify some parts of the prefabs children
And if I'm still not clear enough: I want to initiate a shoe. I want to have access, for example, to the lace Prefabs of the shoe I have initiated. But I also want to have access to other parts individually (Like the sole of it) to be able to color them individually! I already have made the code to color them, I only need to get the way to initiate the shoe and get his children individually to modify them as well!
If you still don't understand me, I can try to make another explanation on demand! Thanks for your help!
Answer by Nazirzadeh · Feb 06, 2018 at 01:50 AM
You can try Transform.Find or GetComponentInChildren
Well, I've tried to simply use them but the thing is that
For the Transform.Find, I will try it! I've never seen it before so I never tried it!
For the GetComponentInChildren, I think it will only refer you to the first children prefabs but I need to have access to five of them every time I instantiate the shoe prefabs.
Thanks you anyway and if I'm wrong on what I say, please tell me how to use them as I want to!
Answer by Legend_Bacon · Feb 06, 2018 at 02:22 PM
Hello there,
If you have a reference to the GameObject, it's very simple. You could do:
public GameObject prefab_shoe = null;
public Vector3 v3_spawnPos = Vector3.zero;
private void CreateShoe()
{
GameObject newShoe = Instantiate(prefab_shoe, v3_spawnPos, Quaternion.identity) as GameObject;
List<GameObject> laces = new List<GameObject>();
List<GameObject> sole = new List<GameObject>();
for (int i = 0; i < newShoe.transform.childCount; ++i)
{
Transform currentItem = newShoe.transform.GetChild(i);
//Search by name
if (currentItem.name.Equals("laces"))
{
laces.Add(currentItem.gameObject);
continue;
}
if (currentItem.name.Equals("sole"))
{
sole.Add(currentItem.gameObject);
continue;
}
//Search by tag
if (currentItem.CompareTag("laces"))
{
laces.Add(currentItem.gameObject);
continue;
}
if (currentItem.CompareTag("sole"))
{
sole.Add(currentItem.gameObject);
continue;
}
//Search by component
if (currentItem.GetComponent<Laces>() != null)
{
laces.Add(currentItem.gameObject);
continue;
}
if (currentItem.GetComponent<Sole>() != null)
{
sole.Add(currentItem.gameObject);
continue;
}
}
// Do something with the results here
if(laces.Count > 0)
{
// Do something with the laces
}
if(sole.Count > 0)
{
// Do something with the soles
}
}
This way, you get every single child that corresponds to our criteria (I'm not sure why you would have multiple soles, but you could have multiple buttons for example).
Hope that helps!
Cheers,
~LegendBacon
Well, if I'm telling I will have multiple soles, it's because I'm going to instantiate multiple shoes during the realtime (With an GUI to spawn them with specific colors!
But thank you anyway! I'll try as soon as I can!
I was wondering: How can we create some new void function (Like the createShoe void you just placed there)?
This is the answer. GetComponentInChildren does not work only gets the components in the first child or at least thats what Im getting
Follow this Question
Related Questions
How to obtain this prefab name in this circunstances 1 Answer
Script on prefab keeps values from one execution to another 1 Answer
Multiple Cars not working 1 Answer
Tower defense target update 0 Answers
How to Find gameObject child. 3 Answers