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: 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.
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.
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?
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>();
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.
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);
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.
Your answer
Follow this Question
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