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 /
  • Help Room /
avatar image
1
Question by phineliner · Oct 22, 2015 at 12:05 PM · prefabscript.inspectorruntime-generation

Assigned Prefab to generic Script in Inspector is lost at runtime - Intended or bug?

Hello there,

I'm currently trying to instantiate a prefab via script. The thing is, that the instance of the gameobject holding the script is generated at runtime. Thus I can not drag the prefab to the component of the gameobject. I know I can load a prefab via Resources.Load but I was wondering why the following approach doesn't work:

I select the script in the Project View (not Hierarchy!), which shows the script in the inspector: alt text There you can see the prefab attribute. So I assumed it would work to assign the prefab to the script in general, independent of the gameobject which will have it as component.

However, on runtime the prefab is null: "ArgumentException: The thing you want to instantiate is null."

Is this the intended behavior, am I missing something, or is this a bug?

Cheers, phineliner

P.S.: It works when I load the prefab via Resources.Load(). So the code seems okay.

=====================================================================

edit: I am aware of how to achieve the desired thing (e.g. attach component and prefab in another prefab or using Resources.Load()). However, my question is:

Is the described behavior intended or a bug?

The placeholder on the script-file looks like it is exactly for this purpose and I can't think of any other purpose. I am wondering why it doesn't work as expected and why the possibility of dragging & dropping something to the script-file directly is given, when it loses (doesnt remember) it on runtime.

script-prefab.jpg (21.1 kB)
Comment
Add comment · Show 9
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 phineliner · Oct 22, 2015 at 05:05 PM 0
Share

has nobody tried this before?

avatar image vintar · Oct 22, 2015 at 05:56 PM 0
Share

I`ve seen this too. I was totally excited when I noticed how you can drag something onto a script, but it is forgotten when you press play. No idea why they even show up there.

avatar image entity476 · Oct 22, 2015 at 08:00 PM 0
Share

If I may ask, is the gameobject holding the Settings$$anonymous$$anager script, also a prefab, which is instantiated in run time? Or, maybe, an empty gameobjec, created on runtime, on which you call the GetComponent(Settings$$anonymous$$anager) function in order to attach the Settings$$anonymous$$enu behaviour?

avatar image phineliner entity476 · Oct 22, 2015 at 08:51 PM 0
Share

Hi .entity. It's an empty gameobject to which I add the Settings$$anonymous$$anager component at runtime.

 GameObject go= new GameObject();
 go.AddComponent<Settings$$anonymous$$anager>();

avatar image entity476 phineliner · Oct 22, 2015 at 09:55 PM 0
Share

Sorry, you're right. "Add" not "Get" is what I meant to write. Why don't you create then a prefab (which you'll instantiate at run time) holding the Setting$$anonymous$$anager script with the Settings$$anonymous$$enu prefab pre-attached and see if that works? Unless all you ask is the reason of why it doesn't work the way you've been trying and, well, in this case I'm unfortunately not able to certainly provide a good answer. (Just thinking out loud: Is it related to serialization?...) I'm also unable to test your scenario right now in order to suggest any workarounds, other than the one you mentioned already, in case you would like so.

Show more comments
avatar image meat5000 ♦ · Oct 23, 2015 at 10:02 AM 0
Share

If you instantiating from a prefab, after you drop the script in to the placeholder, click Apply. If this doesn't work it's apparent that Prefabs dont like remembering such things.

Your AddComponent approach will work, why not use it?

 GameObject myGO = Instantiate(Prefab);
 
 myGO.AddComponent(SomeScript);

avatar image phineliner meat5000 ♦ · Oct 23, 2015 at 10:29 AM 0
Share

Hi and thanks for your suggestion. I'm aware of that your suggestion works. However I was asking why my approach doesn't. I'm not sure if the in the question described behavior is intended or a bug, since the placeholder on the script-file looks like it is exactly for this purpose and I can't think of any other purpose.

0 Replies

· Add your reply
  • Sort: 

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

How to use fill method on prefab object? 0 Answers

How to attach a GameObject to a Prefab that has many scripts attached? 0 Answers

How can I click/ pickup an object from afar? 0 Answers

How can I modify/change a prefab? 1 Answer

Pros and cos of editor vs script for connecting things to game objects. 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