Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 liortal · Sep 21, 2014 at 03:11 PM · c#gameobjectprefabprefabs

How to tell at runtime if a GameObject is a prefab

The GameObject class is used to represent objects that can be instantiated and used at runtime.

As such, a script that has a public GameObject field can hold either an object from the scene (drag it from the scene view), or a prefab (drag one from the project view).

The problem is, at runtime, how can i determine which case it is? For a prefab, i'd like to call Instantiate, to create a real object from it. If it's a scene object, there's no need to create it (since it already exists).

Is there such an API that allows determining this ?

EDIT: giving more context on what i'm trying to achieve.

I am creating a prefab object that serves as a container for other objects. As such, it may contain other objects that were dragged to it from the scene, or prefabs that were dragged from the project view.

At runtime, this container needs to instantiate some of the objects it contains. However, since it stores all objects as GameObject references, it cannot know at runtime whether the object should be created using Instantiate, or whether it already exists in the scene (and so should only be "enabled" or something similar).

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by andsee · Sep 21, 2014 at 04:03 PM

At run time prefabs do not exist they are all converted to game objects losing the link to the prefab. There is therefore no way to tell if an object was indeed a prefab when it was still in edit mode. Perhaps you would like to explain your use case to see if we can suggest a solution?

In my first game we fell into the trap of not realising this and all the links to prefabs in our levels scene which defined the positions of objects across all levels caused all referenced data in the prefabs to be loaded at the start of the game.

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 liortal · Sep 22, 2014 at 07:20 AM 0
Share

Added more information to my question

avatar image
1

Answer by fafase · Sep 21, 2014 at 05:43 PM

A prefab is a game object reference that is stored in memory but the object is not in the scene. So two ways, you can first grab all object already in the scene and place them in a list, then any newly created item is also added. When you need to check iterate to see if your guy is in the list.

Second way, grab all objects in the scene with FindObjectsOfType and do the same.

Depending when the action is supposed to happen, one or the other will do.

Here is an example:

 public class NewBehaviourScript : MonoBehaviour {
     public GameObject prefab;    // Well... the prefab.
     List<GameObject> list = new List<GameObject>(); // Create list

     void Start () {
             GameObject [] objs = FindObjectsOfType<GameObject>(); // Getting existing objects
             list.AddRange(objs);
             list.Add((GameObject)Instantiate(prefab)); // Create new items
             list.Add((GameObject)Instantiate(prefab));
             print (IsPrefab(prefab)); // Is our prefab there?
     }


     private bool IsPrefab(GameObject objCheck){
             foreach(var obj in list){
               if(obj == objCheck){
                  return false;
               }
             }
             return true;
        }
 }

Other way:

 public class NewBehaviourScript : MonoBehaviour {
 public GameObject prefab;
 // Use this for initialization
 void Start () {
     Instantiate(prefab);
     Instantiate(prefab);
     var o = (GameObject)Instantiate(prefab);  // is this object a prefab?
     print (IsPrefab(o));  // Should tell no
             print(IsPrefab(prefab));
 }


 private bool IsPrefab(GameObject checkObj)
 {
     GameObject [] objs = FindObjectsOfType<GameObject>(); // Grab all
     foreach(var obj in objs)
     {
         if(obj == checkObj)
         {
             return false;
         }
     }
     return true;
 }
 }

Second situation may be heavy if you have a lot of objects in the scene. First situation is better then but you need to come up with a basic framework to add your objects in the list when created. (No biggie tough)

Comment
Add comment · 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
0

Answer by VesuvianPrime · Sep 21, 2014 at 03:30 PM

I have asked this question before!

http://answers.unity3d.com/questions/719887/how-to-check-if-a-component-is-on-an-instantiated.html

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 liortal · Sep 21, 2014 at 03:33 PM 0
Share

That solution is only available in the editor... I guess your scenario was more inspector related. But for me, i'd like to know that at runtime (no UnityEditor classes)

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

27 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

Related Questions

Game Object is Instantiating on same position 2 Answers

Spawning a prefab at another object's location 3 Answers

How to create meteors everywhere without making the game slow? 1 Answer

Staged Prefab can't "Apply" after adding using Gameobject as a variable 1 Answer

Instances of one prefab work wrong together,OnMouseOver works wrong with instances of prefabs 0 Answers


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