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 Angmar · Apr 10, 2015 at 10:19 PM · prefabspawnchildrenmeshrenderer

Children of Instantiated Prefabs are not seperate

I have a child called "hairMesh" in two Instantiated prefabs and I refer to these children using Find function.

I'm trying to change the material on both the "hairMesh" children but it only changes the first one. I also noticed that when trying to add the MeshRenderer component on the two children, it only is added to the first one.

I also ran a condition testing if the children are the same and it returns true. No idea why they're the same as the prefabs are spawned separately.

Here's my code:

 var headObj = transform.Find("gut").Find("stomach").Find("chest").Find("neck").Find("headtip").Find("head").Find("headItem");
         var headPrefab = IVendor.items[IVendor.FindItemID(head)];
         if(headPrefab) {
             var headSpawn = Instantiate(headPrefab, headObj.transform.position, headObj.transform.rotation);
             if(headSpawn) {
                 if(headSpawn.Find("hairMesh")) {
                     headSpawn.Find("hairMesh").AddComponent(MeshRenderer);
                 }
                 headSpawn.transform.parent = headObj;
                 headSpawn.name = head;
                 setHeadItem = head;
                 if(selfPlayer) {
                     var headItem = skullObj.Find("HeadItem");
                     var headSpawn2 = Instantiate(headPrefab, headItem.transform.position, headItem.transform.rotation);
                     headSpawn2.transform.parent = headItem.transform;
                     headSpawn2.layer = LayerMask.NameToLayer("UI");
                     headSpawn2.name = head+"_skull";
                     headSpawn2.transform.localScale = headItem.transform.localScale;
                     SetLayerRecursively(headSpawn2, LayerMask.NameToLayer("UI"));
                     if(headSpawn2.Find("hairMesh") == headSpawn.Find("hairMesh")) {
                         Debug.Log("Children match???");
                     }
                     if(headSpawn2.Find("hairMesh")) {
                         headSpawn2.Find("hairMesh").AddComponent(MeshRenderer);
                     }
                 }
             }
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
Best Answer

Answer by NoseKills · Apr 10, 2015 at 11:24 PM

I'm on mobile and can't check this and also my javascrip is a bit shaky compared to c#, but I believe what is happening is:

You don't assign strict types for variables in your code so you can't be sure exactly which methods you are calling.

There is non-static transform.Find() that searches for child objects and there is static GameObject.Find() that searches the whole scene.

You just Instantiate the "headspawn" objects withou caring about their type and apparently end up calling the static GameObject.Find() through the created instances (in c# that's not even possible) instead of the transform.Find(), and so you end up finding the first object called "hairMesh" in the scene both times.

Try changing the xxx.Find("hairMesh") calls to xxx.transform.Find("hairMesh") And regardless of whether that worked or not, switch to C# ;)

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 Angmar · Apr 12, 2015 at 06:40 PM 1
Share

Cheers Nose$$anonymous$$ills, this solved the issue. I'll keep this advice in $$anonymous$$d for the future, apart from that last bit ;)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer

How to deactivate or pause prefab array when pause button is pressed? 1 Answer

My prefabs always spawn twice at different positions 1 Answer

How do you instantiate just one prefab? 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