• 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
Question by JayOhhh · Jul 30, 2014 at 01:41 AM · c#prefabchildforeach

Set the Sprite for a child of a prefab before intstantiation.

I seem to be a bit stumped, I'm hoping you guys might have a better solution or a working one for that matter.

I have a preFab, that is not placed on my scene. I have an empty game object on my scene. Attached to the empty game object is a C# Script. I've declared a public variable for my preFab, and dragged it into the inspector.

The problem I'm having is this preFab has 4 children. The Prefab is a rectangle and the children are doors in the middle of each face (north door, south door, east door, west door).

In my C# script, I need to disable/enable the SpriteRenderer component of the child objects BEFORE I instantiate it. I'm using the following method, I'm trying to iterate through the preFabs children, but the loop is never entered.

Is this becuase the prefab isn't instantiated, so I cannot access the components of it?

     private void SpawnDungeon() {
      
      roomCount = 0;
      doorsToSpawn = Random.Range (0,5);
      roomsToSpawn = Random.Range (minRooms - 1, maxRooms + 1);
 
         
                 // This is what I'm trying to fix.
         SpriteRenderer[] rs = RoomPre.GetComponentsInChildren<SpriteRenderer>();
         foreach (SpriteRenderer r in rs) {
           Debug.Log ("We are inside the loop");
         }    

I've tried a few different things here, and Debug.Log is never called!

Is there another way I should go about this? Basically what I need to do is, enable the sprite renderer of each door, depending if that door should show. This needs to be done before the RoomPre is instantiated.

This is for procedural generation. Should I just use Resource.Load to grab the prefab rather than assigning it as a public variable? Would that allow me to edit the sprite renderers of the children objects of the prefab?

Thanks for your time,

JayOhh Games

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by JayOhhh · Jul 30, 2014 at 06:12 AM

I found the answer, stupid me. I didnt know what to search on here but I found it afterall.

http://answers.unity3d.com/questions/266432/editorhow-can-i-get-components-in-non-instantiated.html

I just needed to put true into the brackets of the get component.

         SpriteRenderer[] rs = RoomPre.GetComponentsInChildren<SpriteRenderer>(true);
 

Apparently doing that allows Unity to grab the non instantiated prefab and pull its children components! Man I feel stupid, such an easy fix.

Comment
jprocha101

People who like this

1 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 jprocha101 · Aug 04, 2015 at 02:14 AM 0
Share

wonderful! that really helped me out. thanks!

avatar image

Answer by Lazdude17 · Jul 30, 2014 at 02:05 AM

if you only have 4 doors you could just run a for loop.

 for(int i = 0; i < 5; i++)
 {
     rs[i].enabled = false;
 }
 //Then here re-enable the doors that need to be there.
Comment
JayOhhh
jprocha101

People who like this

0 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 JayOhhh · Jul 30, 2014 at 03:11 AM 0
Share

The problem is that the rs[i] has nothing in it. The get component isn't returning anything.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Get Children of Child and Deactivate/Activate on Demand 1 Answer

Distribute terrain in zones 3 Answers

[C#] An error occurs when trying to set a child to my GameObject in script. Why? 0 Answers

Multiple Cars not working 1 Answer

Using ForEach Loop to change variables on prefabs 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