- Home /
GameObject variable points to instance instead of prefab when prefab gets instantiated
I have a component called Item which holds a reference to a Prefab. The GameObject this component is attached to is that Prefab, so it's a variable which points to its own Prefab.
The problem is that when I instantiate this prefab, the variable which references the prefab seems to actually change and point to the instance of the instantiated GameObject instead of referencing the Prefab.
I'm assuming this is because it's actually referencing itself and not the prefab object? I have no idea, but is there any way to solve this problem?
Answer by Obsessi0n · Jan 29, 2018 at 07:25 PM
I am not sure what you want but,
theClonedExplosion = Instantiate(explosion,transform.position, transform.rotation);
If you intantiate something this way "theClonedExplosion" will point to the new intantiated object.
I think I explained my issue kinda bad, but in this case it would be the explosion variable which changes from a reference to a prefab to a reference to an instance due to the fact that it was pointing at the Prefab which is being instantiated using that line. I can't seem to explain in a good way :/
What @Commoble said is right there should be another simpler way to do what you want.
Answer by Commoble · Jan 29, 2018 at 07:28 PM
I believe this is intended behaviour. Look at it this way: Your prefab is a GameObject that holds a component, and that component has a field that points to the GameObject that the component is attached to. When you instantiate the prefab, you create a copy of that object, and so the copy also contains a component that points to its own GameObject. Make sense?
If you want a long-term reference to your prefab so you can easily instantiate it at will, you should use a different object to keep a reference to it (also make sure you're not assigning the newly instantiated copy to your field).
Hm yeah that's what I thought was the case.
I don't think I would be able to do that because of how the code works. Basically what I'm creating is an item system, and this solution would require every item in the game to have a second object to reference the prefab, which doesn't seem like a very good approach.
But I guess I might have to redo the system if this problem can't be solved without increasing the amount of objects :/
I was thinking more along the lines of having a single manager object that instantiates all of your prefabs (or at least all prefabs of a related set)
I use a ScriptableObject to hold a set of prefabs and then give a manager gameobject a reference to that scriptable object
Not 100% sure if I understand, but the thing with my current system is that the object which I'm instantiating needs a reference to the prefab it was instantiated from.
I'm trying to make a item system where Item is a normal component and holds a object of type ItemData, which contains a GameObject which is set to the prefab. When the Item gets picked up the ItemData gets copied to the inventory, and the Item GameObject gets destroyed (and since the prefab of ItemData references the instance ins$$anonymous$$d of the prefab it becomes null). That way if the player drops a specific ItemData from the inventory it can be re-instantiated into the scene again. But now it can't since the prefab ins$$anonymous$$d references the instance.
Not sure if that makes any sense or helps at all. $$anonymous$$aybe I should add my current code to the post?
Your answer
Follow this Question
Related Questions
Instantiated prefab references point to first prefab instantiated 3 Answers
Accessing instance of prefab from its own script 1 Answer
Why is it important to create an empty gameobject for my prefabs? 0 Answers
How can I create a prefab variant in C# script? 1 Answer
instantiated GameObject Prefab jumps in air after instantiate 1 Answer