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 refsus · Aug 23, 2013 at 08:30 AM · gameobjectpositionchild

set position child of game object by name

Hi there, I want to set position my child of gameobject based name. How should I do? Here my sample code

 GameObject Parent;
 
 foreach(Transform child in Parent.transform)
 {
      if(child.name == "Child1")
         Child1.transform.position = new Vector3(10, 10, 10);
 }

there is a sample code, I want to transform child of gameobject with name Child1 to x=10, y=10, z=10 thanks for your response :)

Comment
Add comment · Show 7
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 MaT227 · Aug 23, 2013 at 09:02 AM 0
Share

Can you explain your problem. I am not sure to understand what you want. Do you want to set every GameObject with name "Child1" in position 10,10,10 ?

avatar image refsus · Aug 23, 2013 at 09:11 AM 0
Share

yes, but child of gameobject that contains name "Child1"

avatar image MaT227 · Aug 23, 2013 at 09:18 AM 0
Share

I think you can just replace Child1.transform.position = ... by child.transform.position = ... Or is it the child of the child ? :P

avatar image refsus · Aug 23, 2013 at 09:23 AM 0
Share

if I replace with child.transform.position = ..... the whole of gameobject at position x=10, y=10, z=10 in this case I just want to set position that contains name Child1

avatar image MaT227 · Aug 23, 2013 at 09:27 AM 0
Share

So you want to move the child of the game object named "Child1" ?

Show more comments

2 Replies

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

Answer by Eugenius · Aug 23, 2013 at 09:26 AM

Just tested the below in Unity on a new project with a Cube parent and Cube children. It works exactly as you mention you would like to. It's in Javascript though.

Hope this helps!

 #pragma strict
     
     function Start()
     {
         for(var child : Transform in transform)
         {
             if(child.name == "Cube0")
             {
                 child.transform.localPosition.x = 15;
             }
             
             if(child.name == "Cube1")
             {
                 child.transform.localPosition.x = 10;
             }
         }
     }
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 refsus · Aug 23, 2013 at 09:30 AM 1
Share

thanks Eugenius I will try your code :)

avatar image refsus · Aug 26, 2013 at 03:37 AM 1
Share

thanks Eugenius

avatar image MakoBec · Oct 21, 2014 at 01:48 PM 0
Share

thanks Eug localPosition works

avatar image
2

Answer by mattssonon · Aug 23, 2013 at 09:29 AM

You should just change your code to this, to properly access the found child:

 GameObject Parent;
  
 foreach(Transform child in Parent.transform)
 {
      if(child.name == "Child1")
         child.position = new Vector3(10, 10, 10);
 }
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 refsus · Aug 26, 2013 at 03:38 AM 0
Share

Thanks mattssonon :)

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

19 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

Related Questions

Find position of the GameObject this script is attached to. 2 Answers

Set the ragdoll position 0 Answers

I want to move a cube with rotation but I find this problem 1 Answer

Physics.OverlapSphere() not working after using pool of objects 0 Answers

How could I make a gameobject move above my players head during an animation and then delete it? 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