Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by SkorpionX · Jan 29, 2019 at 04:53 PM · programmingprefab-instance

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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 :

  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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fluffy_Kaeloky · Jan 29, 2019 at 06:21 PM 0
Share

Upvoted for the singleton solution. That's what I was about to recommend as well.

avatar image SkorpionX · Jan 29, 2019 at 06:53 PM 0
Share

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 :/ )

avatar image UnityCoach · Jan 29, 2019 at 07:26 PM 0
Share

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.

avatar image
3

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SkorpionX · Jan 29, 2019 at 06:00 PM 1
Share

It's not about instantiating. It's about using them in editor while setting up a scene.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How does unity add a scriptable object as a child of a prefab 0 Answers

RigidBody.It doesn't give speed to my gameObject 3 Answers

Custom Input Manager Menu Reset Keybinds 0 Answers

How do you put a List under an Array item (Hard time explaining) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges