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 /
avatar image
0
Question by MayIAsk · May 03, 2016 at 05:01 PM · scripting probleminstantiateprefabruntimedrag

Put a prefab in Hierarchy without using Instantiate ? (from code)

Hello,

I was wondering if it was possible to put a prefab (as a game object and from code) in the scene without using Instantiate. I mean doing like a drag & drop but from code, without instantiating the prefab in c#. I know that when you put a prefab in the hierarchy by draging it with the mouse, it's not an instance.. I want to do the same but in C# (script). I tried to use new GameObject and set the prefab as a child but it causes an error (data corruption). Any idea ?

Thanks.

Comment
Add comment · Show 2
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 tuncOfGrayLake · May 03, 2016 at 06:47 PM 0
Share

What do you mean it's not an instance when you drag and drop it?

avatar image MayIAsk tuncOfGrayLake · May 04, 2016 at 07:32 AM 0
Share

I mean that I see nothing on the profiler when I drag and drop a prefab into the hierarchy BUT, I do see some action when I do it from code (Instantiate.Awake, produce, copy). I don't want to awake it or something, I just want to create it.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by coolraiman · May 03, 2016 at 05:46 PM

you cant put the prefab in the scene, it will always be an instance of it.

the data corruption error mean that you are trying to modify the prefab and unity wont let you do that.

Also why would you want to avoid instancing a prefab? This is only a reference to the file in your assets, it virtually cost nothing

And finally, when you drag and drop a prefab in your hierarchy, it is an instance of the prefab.

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 Cherno · May 03, 2016 at 05:50 PM 0
Share

Excellent explaination. +1

avatar image Ryanless · May 03, 2016 at 06:11 PM 0
Share

The part with the reference is not true. it's an instance WITH a reference to the prefab, but its not ONLY a reference.

Easiest way to prove: if it would be only a reference every change made to the instance would automaticlly apply to the prefab which it doesnt, you have to click apply (which uses the reference to add the changes to the prefab)

avatar image MayIAsk · May 04, 2016 at 07:24 AM 0
Share

Alright, but when I look into the Profiler, things are different when I instantiate a prefab from code and when I drag and drop it. Also, is there a way to set a prefab as a child of an empty object without modifying the prefab ? I want to avoid instantiating the prefab because I use GAF plugin (flash to Unity) and when I instantiate from code GAF generate a big spike 150$$anonymous$$B, GC alloc but when I just drop it with the mouse, GAF don't call some functions that cost a lot but only those which create the object (5$$anonymous$$B ins$$anonymous$$d of 150$$anonymous$$B). That's why I want to find a way to do the same as the drag and drop do but from code.

avatar image
0

Answer by Ryanless · May 03, 2016 at 06:07 PM

ofc its an instance when you put something in the hierarchy. EVERYTHING in the hierarchy is ( and must be) an instance.

Little detail though, instantiate does cost a lot of cpu usage if you instantiate tons of object (like bullets). Then it becomes more effiecent to use a objectPooler. A great leason about objectPooling.

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 MayIAsk · May 04, 2016 at 07:27 AM 0
Share

I know what polling is, this is not my problem here, I only want one object in my scene but using the same process as a drag and drop which obviously work as same as an instantiate. In my case, setActive cost a lot with the plugin I use.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate object 1 Answer

Instantiate a prefab and then add a animator component and controller to the component. 2 Answers

How to instantiate a Prefab ? 2 Answers

Adding a NetworkView component at runtime 3 Answers

Adding behaviors to a game object at run time via scriptable object 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