x


How can I test whether an object is a prefab or an instance?

Is there a way to test whether an object is a prefab or an instance besides trying to find the same object instanced using one of the FindObject or FindGameObject style methods?

more ▼

asked Dec 28 '09 at 03:10 AM

yo.ian.g gravatar image

yo.ian.g
64 1 1 9

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

Very interesting question ;-) I think there's no way to test that from within your game. But I'm not sure what the use case for such a test would be. In your game (while playing), I think whether an object is a prefab or not doesn't really matter unless you want to instantiate the prefab "again". But even in that case, you could also duplicate existing objects (non-prefabs); and if you want to instantiate a prefab, you need to have a reference to it - and by having that reference you'd know it's a prefab. So, personally, I don't see much use in such a check from within the game - but I might be missing something (feel free to comment or edit your question to add a use case).

In the editor (for editor scripting), it's a completely different story, of course. There, it can be very important to know what kind of object you're dealing with. And in that environment, it's also possible: See EditorUtility.GetPrefabType.

more ▼

answered Dec 28 '09 at 09:08 AM

jashan gravatar image

jashan
10.1k 25 40 116

So if you've got a reference to a GameObject that just happens to be a prefab at runtime it's essentially instanced and you can manipulate it and find it as any other object?

Jan 08 '10 at 10:58 PM yo.ian.g

Yes - during runtime, it doesn't make a difference in the game objects behavior.

Jan 10 '10 at 06:14 PM jashan

excellent, thanks a lot!

Jan 12 '10 at 04:32 AM yo.ian.g

It's useful when changing material of object. If it is main prefab it changes permanently. It will be changed even in Editor.

Apr 11 '12 at 09:03 PM Santa
(comments are locked)
10|3000 characters needed characters left

As of Unity 4, I have found that using

gameObject.activeInHierarchy

has become useful for situations like this should the rare occasion arise.

more ▼

answered Mar 08 at 05:55 PM

dannyskim gravatar image

dannyskim
3.9k 5 7 19

(comments are locked)
10|3000 characters needed characters left

No, but since prefabs have to be instantiated manually it shouldn't be too difficult to keep track of such things yourself.

more ▼

answered Dec 28 '09 at 06:07 AM

Peter Alexander gravatar image

Peter Alexander
931 5 6 23

Sorry, I meant test in code. Since prefabs can be instantiated at design time, outside of runtime code's knowledge, and object references can point to prefabs or instances of prefabs I was hoping there was a way to differentiate.

Dec 28 '09 at 07:10 AM yo.ian.g

I am aware that you are talking about runtime. I was referring to prefabs that you Instantiate through code, which could be wrapped with some of your own code.

Dec 31 '09 at 02:09 AM Peter Alexander
(comments are locked)
10|3000 characters needed characters left

This won't work if you change the name of the game object, but since instantiated objects start with "(Clone)" as part of their name, you can test for that:

if (gameObject.name.Contains("(Clone)")
{
    // not a prefab
}
else
{
    // probably a prefab
}
more ▼

answered Oct 14 '12 at 04:40 PM

Vimalakirti gravatar image

Vimalakirti
37 1 1 5

(comments are locked)
10|3000 characters needed characters left

You can check for a selected prefab like this: [code]private static bool HasSelectedPrefab() { if (Selection.activeObject) { UnityEngine.Object obj = PrefabUtility.GetPrefabObject(Selection.activeObject); if (obj) { return true; } }

    return false;
}[/code]

You can use an editor script to populate a prefab with a listof your prefab references to compare with at runtime.

more ▼

answered Nov 19 '12 at 03:35 PM

tgraupmann gravatar image

tgraupmann
6 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1260

asked: Dec 28 '09 at 03:10 AM

Seen: 3148 times

Last Updated: Mar 08 at 05:55 PM