Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AUsername64 · Jul 31, 2021 at 07:30 PM · uitransformbuttonui image

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rage_co · Aug 01, 2021 at 04:11 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AUsername64 · Aug 02, 2021 at 01:56 AM 0
Share

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.

avatar image AUsername64 · Aug 02, 2021 at 01:58 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

249 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges