- Home /
Determine if an external prefab exists within the project 's assets folder (not in scene)
I am using assets bundle to dynamically add prefabs to a scene and those prefabs can be cloned by the users. When a user clone a prefab, I need to determine if the prefab has been loaded. If not, then I will use the WWW class to load the asset bundles with the prefab.
Question is, how do I know that a prefab is loaded and ready to be clone?
all prefabs will load in startup and you need to just download asset bundles to use.
Answer by LeMoine · Jan 28, 2014 at 01:27 AM
Ok, I found the solution here:
How to "Get" prefabs from project View by code
To check if a prefab is in the assets, use the Resources.Load("prefabName")
method, like this:
Object prefab;
try{
prefab = Resources.Load("prefabName");
}
catch(UnityException e){
Debug.Log (e);
}
if(prefab == null){
//doesn't exist
}else{
Debug.Log("Prefab exists");
}
Pay attention that the prefab has to be in a folder named 'Resources'.
Answer by Chris Masterton · May 29, 2010 at 05:49 AM
If you know the name of the prefab you could try GameObject.Find. A return value of null would mean it is not loaded.
GameObject myPrefab = GameObject.Find("nameOfMyPrefab");
if( myPrefab == null )
{
// Uh oh, my prefab has not loaded.
}
else
{
// We found the prefab, therefore it loaded.
// You may still have to check if its activated -
// I'm a bit unsure on this part!
}
Please post your results!
Your answer
Follow this Question
Related Questions
How to import the object from server to unity 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Storing a part of a complex asset in an asset bundle 1 Answer
cloned game object wont call script right... 2 Answers
Animation controller attached to prefab instance doesn't work 0 Answers