Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by JayFitz91 · Jan 25, 2016 at 04:20 PM · childforeachiterate

Trying to iterate through a prefab to access its child objects

I have a 3 tier building, each tier is made up of one separate piece and these pieces are stored in an empty game object. What I am trying to do is, starting from the bottom, fade the alpha of the object to full, when the bottom one is full, move onto the second one and then the third one etc.

I am using the following code:

 public GameObject basicBuildingFinishedPrefab;
 private GameObject currentlyBuilding;
 private iny currentPart = 0;
     
 if (!isBuilding)
 {
     isBuilding = true;
     currentlyBuilding = Instantiate(basicBuildingFinishedPrefab, transform.position + offset, transform.rotation) as GameObject;
 }
 else
 {
     foreach (GameObject[] child in currentlyBuilding.transform)
     {
         Color temp = child[currentPart].GetComponent<Renderer>().material.color;
         temp.a += 0.5f * Time.deltaTime;
         child[currentPart].GetComponent<Renderer>().material.color = temp;
 
         if(temp.a >= 1)
         {
             currentPart++;
         }
     }
 }

But I am getting the following error:

 InvalidCastException: Cannot cast from source type to destination type.

I am getting it just at the beginning of the foreach loop. Any advice appreciated, probably overlooking something small.

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 Owen-Reynolds · Jan 25, 2016 at 06:51 PM 0
Share

Using "Prefab" is a little confusing here. If you Instantiated something from a Prefab, then it counts as a regular gameObject. You can just search "How to do X on a gameObject."

Usually, Prefab refers to the original prefab in Project. For example, some people want to change the prefab so future Instantiates will have those changes.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Munchy2007 · Jan 25, 2016 at 04:35 PM

If you want to iterate through all children and their children etc. you could approach it with recursion.

Drag this script onto any GameObject and it will debug.log the names of all children no matter how nested they are.

 using UnityEngine;
 using System.Collections;
 
 public class IterateChildren : MonoBehaviour {
     
     // Use this for initialization
     void Start () {
         iterateChildren(transform.root);
     }
 
     void iterateChildren(Transform trans)
     {
         Debug.Log(trans.name); // Do whatever logic you want on child objects here
         if(trans.childCount == 0) return;
 
         foreach(Transform tran in trans)
         {
             iterateChildren(tran);
         }
     }
 }

Or you could also just use

         var children = transform.root.GetComponentsInChildren<Transform>();
         foreach(var child in children)
         {
             Debug.Log(child.name);
         }
 
 


Comment
Add comment · Show 1 · 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 JayFitz91 · Jan 25, 2016 at 05:05 PM 1
Share

This is great, cheers

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

35 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

Related Questions

Child and foreach ? 0 Answers

Using foreach while looking for tag in children 1 Answer

Looking through all the children doesn't work 1 Answer

Change layer for unparented child. 0 Answers

Set parent of an object when they collide? 2 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