- Home /
Instantiating scripts with references to prefabs loses the reference.
So as far as I know, there are 2 ways to attach a prefab reference to a variable in a script:
1) Drag the prefab into the public variable through the inspector; Problem: The reference is lost when the script is instantiated at run time.
2) Load the prefab by string name with resources.load(); Problem: Renaming or moving the script requires you to edit the name or path of the string passed into resources.load() in every location that loads that prefab.
Is there really no way to reference a prefab without having either of these issues?
The best solution I've come up with is to use resources.load but instead of passing in the string directly, pass a string variable containing the string from one globally accessible location (so renaming/moving the prefab means I only have to change the name/path at one location in code). This means that for every gameobject i might want to instantiate at run time, I'd need to add a variable in global scope reference that object's prefab. While this is fine, it just seems a bit dirty, tedius, and unexpected to have to do in unity. I feel like there must naturally be a better way that I just don't know about.
My specific situation: I'm making an rpg. There are items in the form of scripts that are instantiated at run time. Some items need to instantiate gameobjects (like projectiles or art assets like a model of that item). So logically, they need a reference to that gameobject, which is a prefab.
Originally I manually dragged the prefabs the item needs to public variables in that item script through the inspector (which is an easy workflow). But when I instantiate the item at run time, the variables are cleared. The only time it doesn't clear and actually maintain it's value (the reference to the prefab) is when I drag the item into the scene in edit mode. But I need to add them to the scene at run time, not before hand in edit mode.
I'm not clear... why are you adding scripts on their own ins$$anonymous$$d of instantiating prefabs with the scripts already on them?
Because the scripts aren't on the prefabs that they're spawning. They're on a different object doing the spawning through the script.
Answer by DanSuperGP · Jan 28, 2015 at 12:26 AM
One easy solution to this is to have a globally accessible manager object with a list of game objects...
Then you drag all the prefabs you need to have accessible at runtime into that list...
And you have a publicly accessible Dictionary
And then on start, you iterate through the list... and add each game object in the list to the dictionary, using it's name as the key...
Then you have a reference by name to all your prefabs... and you can get references to them by their name from the globally accessible dictionary.
Yeah I thought of this, I'm just hoping there's a better solution. It seems odd Unity has all these features for simple workflow, yet to do something as simple as instantiate a script that instantiates prefabs requires us to make a manager and drag every gameobject spawnable at run time into it.
I figure there's a much simpler solution, something I'm missing or I'm doing wrong.
"I figure there's a much simpler solution, something I'm missing or I'm doing wrong."
No, I don't think so. I've been using Unity for 7 years.
Your options are...
$$anonymous$$ake some GameObject that has all the references to your prefabs, set it up in the inspector, then either have it in the scene to begin with or instantiate it from a prefab ins$$anonymous$$d of AddComponent.
Use Resource.Load
That's pretty much it.