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 JDHansen · Jan 15, 2013 at 07:53 AM · positioninstantiationconfused

Instantiating a Prefab at Local position

Before anyone tells me to use the search bar, know that I have. Nothing I've found has helped. I'm just about ready to tear out my hair over this one...

Here is my code:

using UnityEngine;

using System.Collections;

public class InstantiatePrefab : MonoBehaviour {

 public Transform thePrefab;
 GameObject theObject;
 // Use this for initialization
 void Start () {
     theObject = (GameObject)Instantiate(thePrefab, transform.position,transform.rotation);
     theObject.transform.parent = transform;
     theObject.transform.localPosition = transform.localPosition;
     //Destroy (gameObject);
 }

}

From what I've read, this SHOULD instantiate the object and ensure that it goes to the proper position. Indeed, it does go to my the instantiating object's coordinates, but in WORLD space. I should mention that the object calling this script is a child of a Prefab. Any help would be greatly appreciated!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by EelcoJannink · Mar 16, 2017 at 11:22 AM

A bit old, but :

GameObject obstacle = Instantiate(prefabList[1], this.transform, false ) as GameObject;

the false for instantiateInWorldSpace avoids the prefabs position being concidered worldcoords.

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 MINICURSOR212 · Jun 14, 2018 at 04:12 AM 2
Share

I literally can not thank you enough for this one simple post. I don't care how old it was,; this was all I wanted to know. Every single thread I would go to was the same "You should have read the scripting reference ". THAN$$anonymous$$ YOU!!! Also it wasn't just this one simple transform issue that was taking me so long; I was working on other things but still, thank you so much dude.

avatar image LagSik · Dec 03, 2021 at 04:02 PM 0
Share

I cannot thank you enough for the comment. That solves my problem.

avatar image
2

Answer by ilusja · Dec 05, 2018 at 04:41 PM

Hey, it works for me. Script attached to object will be a parent.

 var go = Instantiate (prefab, transform.position + new Vector3(x, y, z), Quaternion.identity);
 go.transform.parent = transform;

The important part is: you have to add transform.position + before the new position so your code can actually use parent's transform.position before placing new object. Notice, that the parent actually becomes the parent after instantiating so that's why it uses world position to instantiate the object.

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 unityDevloper · Mar 16, 2017 at 08:32 PM

May be you can do like this...

 theObject = Instantiate (currentPillar) as GameObject; 
 theObject .transform.parent = parentObject.transform;
 theObject.transform.localPosition = yourPosition;


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
0

Answer by senad · Jan 15, 2013 at 09:45 AM

Maybe I am not understanding you right. :)

But the child's local position is relative to the parent's world position. So if you want the child to be at the same spot as the parent, you just have to set its local position to the zero vector.

If you change that vector, the child will move, but relative to the parent. You can just experiment with the values in the editor to understand what they mean. :)

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 JDHansen · Jan 15, 2013 at 05:09 PM 0
Share

I've tried that as well. I think Unity might be getting confused since the object running the script is already a child of another object. Said object is very close to the zero vector within its parent, so Unity instantiates the object close to the world's zero vector. However, I thought that changing the local position after this happened would fix that problem.

avatar image JDHansen · Jan 15, 2013 at 05:17 PM 0
Share

Hmm... Well, this is strange. It would seem that the object IS instantiating where I want it, but its transformation gizmo is staying at the world zero vector...

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

14 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

Related Questions

Instatiation on the wrong position 1 Answer

Camera rotation around player while following. 6 Answers

How to instantly move a newly instantiated prefab? 2 Answers

instantiated object not in right position 1 Answer

Unity 5.6 doesn't instatiate object mantaining child position driven by animations 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