• 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 kasquer · Oct 06, 2016 at 10:07 AM · javascriptinstantiatetransformprefablocalspace

Instantiate prefab as child at a transform? (Javascript/Uniyscript)

I am having a problem with Instantiating a prefab at a transform location. I asked a similar question yesterday about how to Instantiate prefab at a transform but the answer I got seems to require me to give the location of spawn with Vector3 and Quaternion. I am unsure on how to get these to for local space as I don't want it to spawn somewhere random in the world. Yesterdays Question

Current script:

 var Weapon1 = Instantiate(GunType, Weapon1Spawn.transform.position, Weapon1Spawn.transform.rotation, Weapon1Spawn); 

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 Namey5 · Oct 06, 2016 at 10:11 AM 0
Share

Instantiating with a parent should use local space you don't need to specify a position;

 Instantiate (prefab, parent.transform);    //Should work

Something to point out is that parent instantiation was only introduced in Unity 5.4, so check that your version of Unity supports it.

avatar image kasquer Namey5 · Oct 06, 2016 at 11:04 AM 0
Share

Yeah I can't use it "$$anonymous$$ Identifier "parent"" Is there another way?

avatar image txzeenath kasquer · Oct 06, 2016 at 11:20 AM 0
Share

If you want it at weapon1Spawn, just use that: Weapon1 = Instantiate(GunType, Weapon1Spawn.transform);

Which is the same as dragging it from your prefabs onto "Weapon1Spawn" in the editor.

If you want to make 100% sure it's straight and aligned (it should be) you can then do(might be slightly different in UnityScript):
weapon1.transform.localPosition = new Vector3(0,0,0);
weapon1.transform.localRotation = Quaternion.identity;

Show more comments
avatar image kasquer Namey5 · Oct 06, 2016 at 12:49 PM 0
Share

Getting this error using that: http://image.prntscr.com/image/437e04d5f7004f89a604b9078cf63671.jpeg

avatar image Namey5 kasquer · Oct 06, 2016 at 11:37 PM 0
Share

And you are using at least Unity 5.4? This stuff was introduced very recently, and so if you are using an older version of Unity you will have to set it afterwards.

Show more comments

2 Replies

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

Answer by txzeenath · Oct 06, 2016 at 01:07 PM

You can also just set the parent manually after instantiating:

Weapon1.transform.parent = Weapon1Spawn.transform;

(I believe you can also use Weapon1.transform.SetParent)

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 KreeperGaming · Oct 07, 2016 at 12:27 AM

I'm no expert, but maybe just start it non-active in that transform, and then when called, just activate it.

That is what I do for most of my projects.

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 EpiFouloux · Oct 07, 2016 at 05:13 AM 0
Share

That's the opposite of instantiation..

avatar image kasquer · Oct 07, 2016 at 09:12 AM 0
Share

If I was using 1 gun type I wouldn't need any kind of instantiation but I am making it so it spawns prefab that is set as "GunType"

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The variable othertransform of Prefab has not been assigned 2 Answers

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Simple Instantiate gameObject problem 3 Answers

Problem with GetComponentinChildren and Instantiate 0 Answers

How to follow multiple clones positions of an instantiate prefab having a velocity ? 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