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 /
This question was closed Jul 18, 2017 at 03:16 AM by agencebrunel for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by agencebrunel · Jul 18, 2017 at 02:16 AM · instantiateprefablocalposition

How use local position of prefab for my instantiate ?

Hi, i make a room generator. My room " piece " are in the right position, fixed by my first script :

 private var leveltaille = 75;
 private var nbpiece : int;
 public var piece: Transform;
 
 function Start () {
     nbpiece = Random.Range(6,12); 
     for (var i: int = 0; i < nbpiece; i++) {
     Instantiate(piece, new Vector3(Random.Range(-leveltaille, leveltaille), 0, Random.Range(-leveltaille, leveltaille)), Quaternion.identity);
     }
 }

Then my second script instanciated many block in a grid and put them as child of my prefab piece :

 private var PieceLargeurTaille : int;
 private var PieceLongueurTaille : int;
 var piece : GameObject;
 var block : GameObject;
 var spacing = 2.0;
 
 function Start () {
 
     PieceLargeurTaille = Random.Range(4,8);  
     PieceLongueurTaille = Random.Range(5,8); 
 
     for (var y = 0; y < PieceLargeurTaille; y++) {
         for (var x=0;x<PieceLongueurTaille;x++) {
             var pos = Vector3 (x, 0, y) * spacing;
             var mapiece = Instantiate(block, pos, Quaternion.identity) as GameObject;
             mapiece.transform.parent = transform;
         }
     }
 }

But something wrong for me, the block are positioned with world not "piece" prefab. How can i do this ? Thanks for help.

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 agencebrunel · Jul 18, 2017 at 02:48 AM 0
Share
  • I think i need to make 3 var, for x y and z and find a way to have the position of my gameobject inside.

  • Then add the number in the vector 3 position on my block.

I continue to search...

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jul 18, 2017 at 03:09 AM

To positioning an instantiated object in local coordinates you can do the following:

Either

 var mapiece = Instantiate(block, pos, Quaternion.identity) as GameObject;
 mapiece.transform.SetParent(transform, false); // note the "false"

or

 var mapiece = Instantiate(block) as GameObject;
 mapiece.transform.parent = transform;
 mapiece.transform.localPosition = pos;
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 agencebrunel · Jul 18, 2017 at 03:16 AM 0
Share

Year thanks you so mutch ! i understand the 2 answer, very interesting.

Follow this Question

Answers Answers and Comments

94 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 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 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

Changing localPosition gives no error, but doesnt change prefab localPosition 0 Answers

GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers

Instantiate Object At Local Position 2 Answers

Instantiated Object being auto-deleted 0 Answers

Issue Instantiating prefab in C# 0 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