|
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?
(comments are locked)
|
|
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. 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)
|
|
As of Unity 4, I have found that using has become useful for situations like this should the rare occasion arise.
(comments are locked)
|
|
No, but since prefabs have to be instantiated manually it shouldn't be too difficult to keep track of such things yourself. 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
Dec 31 '09 at 02:09 AM
Peter Alexander
(comments are locked)
|
|
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:
(comments are locked)
|
|
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; } } You can use an editor script to populate a prefab with a listof your prefab references to compare with at runtime.
(comments are locked)
|
