• 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>();

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Can't drag objects into the script's Object Inspector public variables 1 Answer

One specific script won't appear as a component in the Inspector only in Game Mode?? 0 Answers

Applying prefabs 0 Answers

Prefab is "None (Game Object)" on runtime 2 Answers

What is different when Instantiate set Prefab Position? 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