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

People who like this

0 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 "Unknown 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
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
kasquer

People who like this

1 Show 0 · 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

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
EpiFouloux

People who like this

-1 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

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

Instantiate a Prefab thats been passed through 2 scripts? 2 Answers

Why Transform and not Prefab? 2 Answers

(Unity 2D C#) Move instantiated prefab on Y axis? 1 Answer

Get transform of instantiated Prefab; Help appreciated. 1 Answer

Instantiate prefabs before it comes into view 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