- Home /
How to determine if a game object is a generated prefab using code
I am trying to add a script to all game objects in a scene. I have gameobjects created in unity and gameobjects imported from 3d model software. The ones imported from a 3d model software makes the console get mad and say "You cant add script to this gameobject because its a generated prefab and can only be modified through an AssetPostProcessor" How can I make code to check if the gameobject is a generated prefab at runtime?
You don't need to use any code to check, just look at the icon and be aware of what you're doing. Unity displays imported models as blue prefab icons with a little white sheet of paper in the bottom right corner. It sounds like you're trying to drag scripts onto objects that are selected in the project window.
In order to take a 3d model and add components to it you need to drag it out into the scene, add you components, and then drag the object from the hierarchy back into the project view (into something like a prefab folder for organizational purposes).
Addendum: Last section on this page, entitled "Imported Prefabs".
Thank you for attempting to help! However, that was not what I was asking for at all. Very good try, I appreciate it and keep up the good work.
Sorry it wasn't what you were looking for, but there's no runtime code that can detect a difference. There may be something in the UnityEditor namespace, but I'm uncertain.
In my $$anonymous$$d it boils down to what you're trying to accomplish. You shouldn't ever be using the raw imported prefabs in a production environment, so it should never be an issue.
Answer by Statement · Dec 26, 2013 at 12:52 AM
I'd try PrefabUtility.GetPrefabType.
It will return you a PrefabType and I think the following is what you are after:
ModelPrefab
ModelPrefabInstance
DisconnectedModelPrefabInstance
Hope it helps, haven't used that API before. It would be nice if you report back with findings to help document the usage of this API.
That worked great! It worked exactly how it said in api. However, the api is kinda outdated because http://docs.unity3d.com/Documentation/ScriptReference/PrefabType.html
refers to EditorUtility.GetPrefabType when ins$$anonymous$$d you must use PrefabUtility.GetPrefabType(gameObject).
Statement! I know this is not the forum for personal info, but what motivates you to help so many people out what have you done with Unity?
@emc233: Find me on IRC and we can have a private chat if you want to learn more about me. I'm Statement on #unity3d in FreeNode
Im european and it's getting a little late, so if you miss me today I may be on a different time. It's 4 am where I am staying at present.