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?

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 :

  1. add a static member, the same type as the script
  2. in Awake (), if it’s null, assign this to it
  3. if it’s not null, there’s one already, you then use Destroy(this.gameObject)

Hope this helps.

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)”.


Ok, let me proceed backward from the end goal, which is preventing duplicated Player Prefab in a Scene.

My 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 GameManager 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 CheckForMultiplePlayersInCurrentScene ()

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 ("More 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.