- Home /
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).
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.
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)
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
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
Follow this Question
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