So I have a game where when I enter room I want the room to be reset exactly the way it was when the player entered it for the first time.
For example the player could have destroyed a pot, and when he leaves and re-enters the room, the pot must be there again.
I can’t use scenes for this, just because that’s not the way I decided to build my game.
So instead I want to use Prefabs.
My first idea was to create a parent to the gameObject at runtime, this parent has the prefab link and will instantiate a new prefab when the player enters the room.
This works but it’s tedious to have to create 2 prefabs for every object and it makes my project be a mess over time.
(One prefab = the parent that instantiates the destroyable, and the other prefab = the destroyable).
How can I do this with a single prefab + easy to setup?
There is no reason your single prefab can’t contain everything and you simply turn off the parts you don’t want rendered with a reset script that reverts it back to it’s normal state. If you use an interface, then you simply need to gather all objects in the room with that interface, run the reset script, and you’re good to go. Another way would be to just add the objects that get destroyed to a list, then when the player leaves the room, reset the objects, clear the list so it’s ready for the next room.
I’m just guessing on how things are setup, so it’s just ideas.
The problem with this is that every object could be different, resetting stuff through script is work compared to instantiating a new prefab. It’s also safer.
This is what I do. However my problem has nothing to do with triggering the event of being reset. It’s the reset process itself that is the issue.
I’m not sure I follow this 2-prefab issue. Why do you need a parent to instantiate the destroyable? Why not make the destroyable prefab a child of the room prefab directly?
It looks like a prefab cannot hold itself as a reference in one of its components.
In the inspector, the blue icon (representing the gameobject is a prefab) turns grey as soon as I enter playmode.
It’s no longer a prefab. Of course, other prefab references that are not the object itself stay blue however.
I think that might have been why I went with having 2 prefabs in the first place but I’m not sure anymore.
EDIT: I don’t know if what you meant is that I should have the prefab references on the Room gameobject but if that’s what you mean, then it’s not very practical for a level designer.
I would much prefer having the prefab reference on the destroyable directly.
If you want an object to have a reference to its own prefab, you just set the reference at runtime right after you instantiate it. Something like below.
public SomeScriptOnPrefab : MonoBehaviour
{
[HideInInspector]
public GameObject MyPrefab = null; //reference to its own prefab goes here, set at runtime
}
Whatever script spawns the GameObject from a prefab:
Because I want the prefab to show in my scene view that’s not really an option for me.
Using scriptable objects really is the best option in my case.
Here’s the solution I’m using: In editor: I have a pot with the ScriptableObject that has a reference to itself. I can see the pot in the scene view because it’s not instantiated at runtime. In playmode: The pot creates a new gameobject that becomes its parent, this parent gets the prefab reference. If the pot is destroyed, then this parent will instantiate a new pot when the player enters the room.
1 prefab + 1 scriptable object solution. This way a designer cannot place the wrong prefab in the scene.
Why dont you store the prefabs in a scriptableobject when you start the game?
Refresh the data in that room with the data in that object when you re-enter it?
Or find the referenced objects in the room inside the object, applying the data from the prefab back into the scene object