- Home /
I need to alter each child object individually
So I'm trying to change the picture used for each button on a grid in an instantiated image prefab. The only way I can think of changing them will make them all the same but I want them all to be different. Any suggestions?
var children = transform.root.GetComponentsInChildren<Transform>();
foreach (var child in children)
{
Debug.Log(child.name);
if (child.name.Contains("XxX"))
{
child.GetComponent<Image>().sprite = newImage;
}
if (child.name.Contains("YyY"))
{
child.GetComponent<Text>().text = Convert.ToString(1);
}
}
Sidenote: each button has been named "XxX" and the text on each button was changed to "YyY". This was done so as to prevent the code from changing the text and appearance of other buttons on the same page since the code effects EVERY child... if there's a way around that I would like to learn as well, if I can do both with one change all the better.
maybe create an array if images and then instead of a foreach loop (it can work, but i dunno why Array.FindIndex doesn't always return the correct index for some reason).....use a for loop and and apply an image from that array to the child, this will require separating the text and buttons tho
Answer by rage_co · Aug 01, 2021 at 04:43 AM
Here is what i came up with
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ChildFinder : MonoBehaviour
{
public Sprite[] sprites;
[HideInInspector]
public Transform[] buttons, texts;
void Start()
{
Transform[] children = transform.root.GetComponentsInChildren<Transform>();
List<Transform> buttonsList = new List<Transform>();
List<Transform> textList = new List<Transform>();
foreach(Transform child in children)
{
if(child.GetComponent<Button>() != null)
{
buttonsList.Add(child);
}
else if(child.GetComponent<Text>() != null)
{
textList.Add(child);
}
}
buttons = buttonsList.ToArray();
texts = textList.ToArray();
for(int i=0; i < buttons.Length; i++)
{
Transform button = buttons[i];
button.GetComponent<Image>().sprite = sprites[i];
}
for(int i=0; i < texts.Length; i++)
{
Transform text = texts[i];
text.GetComponent<Text>().text = "1";
}
}
}
This script separates Buttons from text too and you don't need to add XxX or YyY to their names, plus it gives the buttons distinct images from an array (it should have the same number of elements as there are buttons, or it'll throw an index out of bounds error ....Hope this helps!
Thanks for the reply! I probably should have posted my own answer sooner but I actually figured out how to get the script to work fine just a couple hours after posting. I hadn't gotten it to exactly where I wanted it to be yet though so I opted not to post it because I figured I just wouldn't get a response, sorry about that. I'm also a bit new to code so I can't tell exactly how this code worked, that might also be my fault since I only posted a small relevant portion of my full code so it's a bit difficult to get the exact concept of what I was looking for.
Here is the full though still incomplete piece of code as it currently stands. (I left void update in because I do plan on using it, I just haven't gotten to that part yet)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
public class ChunkInstantiate : MonoBehaviour
{
//Default image in case none is cast.
public Sprite newImage;
//Used to deter$$anonymous$$e number of instantiated objects
public int objectNumber;
//Used to hold object prefab
public GameObject myPrefab;
//Used to parent prefab to canvas
private GameObject myObject;
public Transform myCanvas;
// Start is called before the first frame update
void Start()
{
//Typecast path into displayable image
Texture2D tex = Resources.Load("DisplayImages/sunset") as Texture2D;
Sprite newImage = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f));
//Instantiates prefabs as chunks
for (int i = 0; i < objectNumber; i++)
{
//Parents prefab to canvas so it appear visible
myObject = Instantiate(myPrefab);
myObject.transform.SetParent(myCanvas);
myObject.transform.localScale = new Vector3(1, 1, 1);
//Variables estalished to iterate through each individual image
int j = 0;
var children = myObject.GetComponentsInChildren<Transform>();
//For loop to effect each child image individually
foreach (var child in children)
{
Debug.Log(child.name);
if (child.name.Contains("XxX"))
{
child.GetComponent<Image>().sprite = newImage;
}
if (child.name.Contains("YyY"))
{
child.GetComponent<Text>().text = Convert.ToString(j);
j++;
}
}
}
}
// Update is called once per frame
void Update()
{
}
}
Your answer
Follow this Question
Related Questions
Converting to "Rect Transform" permanently deleted part of my project 0 Answers
Script Repositions UI Element in the Wrong Spot 0 Answers
I want to make responsive homescreen. how to make responsive homescreen UI like this example ? 0 Answers
How do you handle a scaledown animation on a UI button? 0 Answers
UI image with button component not tracking mouse correctly 1 Answer