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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by LINKeRxUA · Jan 27, 2013 at 01:13 PM · javascriptinstantiatepositionchild

How to get access to child's objects of instantiated prefab on JS?

Have created in "Resources" folder "Body.prefab" and "Obj.prefab". Body.Prefab Hirerarchy is

 "Body" (GO with mesh)
 |_"1st-Dummy" (GO without mesh)
 |_"2nd-Dummy" (GO without mesh)
 |_"3rd-Dummy" (GO without mesh)
 |_"4th-Dummy" (GO without mesh)

need to get after instantiating

 "Body" (GO with mesh)
 |_Objects (Empty GO)
 | |_first
 | |_second
 | |_third
 | |_fourth
 |
 |_"1st-Dummy" (GO without mesh)
 |_"2nd-Dummy" (GO without mesh)
 |_"3rd-Dummy" (GO without mesh)
 |_"4th-Dummy" (GO without mesh)

use JavaScript to instantiate Body prefab

 var Body : GameObject;
 var first : GameObject;
 var second : GameObject;
 var third : GameObject;
 var fourth : GameObject;
 function Start () {
 Body = Instantiate(Resources.Load("Body.prefab", GameObject));
 Body.Transform.Position = Vector3.Zero;
 Body.Transform.Rotation = Quaternion.identity;
 
 first = Instantiate(Resources.Load("Obj.prefab", GameObject));
 }

  1. Can I load prefab and give it another name that propose me unity3d? (It calls "Body(clone)", I want to call it "Body")

  2. How Can I set transform position of first variable to child postion of 1st-Dummy of Body instantiated prefab.

  3. How can I to put first variable to be child of Body?

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

3 Replies

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

Answer by StephanK · Jan 27, 2013 at 01:27 PM

  1. loadedPrefab.name = "Body";

  2. first.transform.position = Body.FindChild("1st-Dummy").position; (Although it would be better to have a direct reference to your dummies.)

  3. first.transform.parent = Body.transform;

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

Answer by LINKeRxUA · Jan 28, 2013 at 11:43 AM

  1. first.transform.position = Body.FindChild("1st-Dummy").position; (Although it would be better to have a direct reference to your dummies.) Doesn't work. Show's me error that can fing in UnityEngine "FindChild"

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 Gizmoi · Jan 28, 2013 at 02:28 PM 0
Share
 first.transform.position = Body.transform.Find("1st-Dummy").position;
avatar image LINKeRxUA · Jan 30, 2013 at 01:56 PM 0
Share

Thank you!

avatar image
0

Answer by moeid · Dec 08, 2013 at 09:46 AM

hi. i think this is the best way to access to the child of a prefab that instantiated.

using UnityEngine; using System.Collections;

public class CreatNewPlanet : MonoBehaviour { public Transform myprefab;

 private Transform NewGameObject;
 TextMesh xmesh=new TextMesh();
 // Use this for initialization
 void Start () {
 
 }
 bool clicked=false;
 // Update is called once per frame
 void Update () 
 {
     if(!clicked)
     {
     if(Input.GetKey(KeyCode.T))
     {

         NewGameObject=Instantiate(myprefab,new Vector3(12,-3,0),Quaternion.identity) as Transform;
             clicked=true;
             print(NewGameObject.GetChild(4).name);
             xmesh=NewGameObject.GetChild(4).GetComponent(typeof(TextMesh)) as TextMesh;

             xmesh.color=Color.blue;
             xmesh.text="------X:"+ ((int)(NewGameObject.GetChild(3).transform.position.x)) .ToString ();



         }

     
 }
 


 }

}

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

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

12 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

Related Questions

Problem with Parent and Child using LookAt 0 Answers

Checking the distance between an instantiated object and an existing object 1 Answer

How to copy a Script as a child of another object 1 Answer

Instantiate as a child at position 2 Answers

Check if child exists and instantiate as child 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