- Home /
Allow only one instance of prefab on scene
I there a way to allow existence of only one instance of a prefab on single scene?
Answer by UnityCoach · Jan 29, 2019 at 05:10 PM
First, you need to know that Prefabs aren't the same at runtime and in the Editor.
In the Editor, they behave like Xrefs that you can track references of using the PrefabUtility and AssetDatabase classes.
At runtime, they're nothing but serialised game objects. PrefabUtility is an Editor class. Which means you can't track the original prefab reference at runtime.
So, if this is something you want to prevent in the Editor, you can go with PrefabUtility and AssetDatabase classes, as well as Selection. You should have everything you need to check for duplicated instances.
If you want to do it at runtime, then look for Singleton implementations, there are different forms.
The usually come down to :
add a
static
member, the same type as the scriptin
Awake ()
, if it'snull
, assignthis
to itif it's not null, there's one already, you then use
Destroy(this.gameObject)
Hope this helps.
Upvoted for the singleton solution. That's what I was about to recommend as well.
It is not a runtime issue for me. I try to prevent my $$anonymous$$mmates from setting multiple players on the scene as it causes some runtime issues. PrefabUtility just creates a prefab from scene which doesn't help in this case. I'm not sure how could I use AssetDatabase and Selection, could you suggest something? (I know singleton, I wish it was that simple :/ )
Ok, let me proceed backward from the end goal, which is preventing duplicated Player Prefab in a Scene.
$$anonymous$$y first reaction would be : why do you not instantiate the Player from code in the first place ? We usually place (re)spawn points in a scene, then a Game$$anonymous$$anager class (often a Singleton) will pick up the first one and Instantiate a Player Prefab at this location, upon Start ().
That said, if you want to prevent several or too many instances of a Prefab in a Scene, you first need a static method to check for the multiple instances.
public static class ProjectGuidelinesCompliance
{
public static void CheckFor$$anonymous$$ultiplePlayersInCurrentScene ()
{
}
}
You then need a callback to hook it up to, so that it happens when the scene changes, or when a new game object is added, or when the scene is saved. You may use EditorApplication.hierarchyChanged for example. The InitializeOnLoadAttribute allows you to hook your method to this callback without any specific action from the user. You may also use SceneClosingCallback or SceneSavingCallback.
Then, you need to define what it is you want to check against. Several instances of the same Prefab, or simply several instances of the same scripted component for example. The latter is much simpler. You may just do something in the lines of :
if (FindObjectsOfType<CharacterController>().Length > 1)
Debug.LogWarning ("$$anonymous$$ore than one CharacterController is present in the scene...");
Otherwise, you'll have to reference the Prefab asset, and check every game objects against it. AssetDatabase.Contains will help you tell if the game object is a Prefab. Then passing a game object to PrefabUtility.GetCorrespondingObjectFromOriginalSource will let you find the original Prefab. You will need to provide a hardcoded reference path to the Player Prefab. But you could store this in an Editor Preferences panel.
Hope this helps.
Answer by tormentoarmagedoom · Jan 29, 2019 at 05:02 PM
Good day.
There is not a function for this, but you can do it by your own.
Before instantiate, you can check if already exist a gamobject with the specific tag, or containing its name, like "PrefabName (Clone)".
Bye!
It's not about instantiating. It's about using them in editor while setting up a scene.