Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 tharinduwika · Jan 16, 2021 at 12:51 PM · prefabtagprefab-instance

Change image visibility of newly instantiated prefab?

In my prefab, there are three stars to change visibility. They are tagged by 'image1', 'image2' & 'image3'. The prefab is attached to the game object called 'levelObject'. When I'm instantiating, create an instance called 'newObject' and I need to change the image visibility inside the for-loop. Hos should I do this?

GameObject.FindWithTag("image1") doesn't work. It can be used for entire prefabs. But not for a specific prefab.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PopulateGrid : MonoBehaviour
 {
     public GameObject levelObject;
 
     Save save = new Save();
 
     private static bool isThisFirstTime = false;
 
     void Start()
     {
         if (!isThisFirstTime)
         {
             Debug.Log("First time");
             save.firstTimeInitialization();
             isThisFirstTime = true;
         }
         Populate(save.Loaddata());
     }
 
     void Update()
     {
         
     }
 
     void Populate(LevelObject[] levelArray)
     {
         GameObject newObject;
         for (int i=0; i< levelArray.Length; i++)
         {
             newObject = (GameObject)Instantiate(levelObject, transform);
             newObject.GetComponentInChildren<Text>().text = levelArray[i].levelId;
             if (i <= levelArray.Length)
             {
                 if (levelArray[i].LevelPass == "1")
                 {
                     newObject.GetComponentInChildren<Button>().interactable = true;
                     if (levelArray[i].LevelStars == "0")
                     {
                         newObject =  GameObject.FindWithTag("Respawn");
                         Debug.Log("no star to play");
                     }
                     else if (levelArray[i].LevelStars == "1")
                     {
                         Debug.Log("1 star to play");
                     }
                     else if (levelArray[i].LevelStars == "2")
                     {
                         Debug.Log("2 star to play");
                     }
 
                     else
                     {
                         Debug.Log("3 star to play");
                     }
                 }
             }
             else
             {
                 Debug.Log("no star to play");
             }
         }
     }
 }
 







Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by overfile · Jan 16, 2021 at 01:54 PM

Hi! You can use the newobject variable that contains a pointer to the new object you have created (cloned from the prefab you have). In each turn of the loop, the newobject variable is changing its value by the new created object, so you only have to call in each turn the components that you want to change the visibility.

Example:

 GameObject[] childImages = newobject.GetComponentsInChildren<Image>();
 
 foreach(GameObject instantImage in childImages)
 {
     if(instantImage.tag == "image1")
         instantImage.SetActive(true);
     else
         instantImage.SetActive(false);
 }

So the function Populate it could be for example like this:

 void Populate(LevelObject[] levelArray)
         {
             GameObject newObject;
             for (int i = 0; i < levelArray.Length; i++)
             {
                 newObject = (GameObject)Instantiate(levelObject, transform);
                 newObject.GetComponentInChildren<Text>().text = levelArray[i].levelId;
                 GameObject[] childImages = newobject.GetComponentInChildren<Image>();
                 if (i <= levelArray.Length)
                 {
                     if (levelArray[i].LevelPass == "1")
                     {
                         newObject.GetComponentInChildren<Button>().interactable = true;
                         if (levelArray[i].LevelStars == "0")
                         {
                             foreach (GameObject instantImage in childImages)
                             {
                                 instantImage.SetActive(false);
                             }
                         }
                         else if (levelArray[i].LevelStars == "1")
                         {
                             foreach (GameObject instantImage in childImages)
                             {
                                 if (instantImage.tag == "image1")
                                     instantImage.SetActive(true);
                                 else
                                     instantImage.SetActive(false);
                             }
                         }
                         else if (levelArray[i].LevelStars == "2")
                         {
                             foreach (GameObject instantImage in childImages)
                             {
                                 if (instantImage.tag == "image2")
                                     instantImage.SetActive(true);
                                 else
                                     instantImage.SetActive(false);
                             }
                         }
 
                         else
                         {
                             foreach (GameObject instantImage in childImages)
                             {
                                 if (instantImage.tag == "image3")
                                     instantImage.SetActive(true);
                                 else
                                     instantImage.SetActive(false);
                             }
                         }
                     }
                 }
                 else
                 {
                     Debug.Log("no star to play");
                 }
             }


It is an example based on your function, the ideal would be to reduce the number of times you do the loop to check which image you have to activate.

Comment
Add comment · Show 3 · 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 tharinduwika · Jan 16, 2021 at 06:07 PM 0
Share

Thank you very much for your valuable comment. This is what I exactly looking for. But there is an error in this line.

 GameObject[] childImages = newobject.GetComponentInChildren<Image>();

It says.

Error CS0029 Cannot implicitly convert type 'UnityEngine.UI.Image' to 'UnityEngine.GameObject[]

avatar image overfile tharinduwika · Jan 18, 2021 at 10:32 AM 1
Share

Sorry, the correct syntax is:

 Image[] childImages = newobject.GetComponentsInChildren();

and for active/deactive the Image, you can use:

 instantImage.gameObject.SetActive(true);

or

 instantImage.gameObject.SetActive(false);


avatar image tharinduwika overfile · Jan 20, 2021 at 11:58 AM 0
Share

Thank you. I solved that issue.

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

150 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

Related Questions

How to hide a character prefab 1 Answer

How to instantiate prefabs with scripts attached? 1 Answer

Reset script to prefab values 0 Answers

Hiding gameobjects in hierarchy make it less expensive ? 0 Answers

New prefab system doesn't allow buttons to work well. 0 Answers


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