- Home /
Trying to load a prefab but getting null error
I created a 3d object using Blender and imported it in Unity as .blend file.
I used this object to create a prefab as described in the Prefabs page in the Unity manual. And i believe that i did that correctly because the prefab icon is blue so it contains the object.
Now, i am trying to instatiate this prefab at runtime. I have an imageTarget and i want to clone this prefab many times and show it above that imagetarget.
The error i get is "The prefab you want to instatiate is null".
This is how i am trying to instatiate my prefab and add it as a child to the imageTarget:
GameObject imageTarget;
imageTarget = GameObject.Find("ImageTarget");
Transform marker = (Transform) Instantiate(Resources.Load("Marker"), new Vector3 (XY[0], XY[1],0), Quaternion.identity);
marker.parent = imageTarget.transform;
The script that has the above code is attached to my ImageTarget prefab.
Thanks in advance for helping! I' ve seen this question in the Answers section but i couldn't find a solution.
Answer by JcDenton86 · Aug 28, 2012 at 11:21 AM
Actually i think i figured it out..I had placed the prefab in a folder called MyPrefab. When i renamed that folder to Resources the error gone!
Anyway, thank you and appreciated your effort! It is great to be in a community like this one
Answer by BimSekai · Aug 28, 2012 at 10:36 AM
When you drag the prefab from your assets to the hierarchy, does it show correctly ?
Yes, when i drag it to the hierarchy i can see the object in the scene and the prefab is blue. Is the code above correct or there is any other way of doing this? Thank you
Answer by BimSekai · Aug 28, 2012 at 11:02 AM
There is another way of doing this, using the editor, adding
public GameObject marker;
and dragging the prefab in the inspector. then you can instantiate it:
Transform myMarker = (Transform) Instantiate(marker, new Vector3 (XY[0], XY[1],0), Quaternion.identity);
Did it help you ?
Answer by Kryptos · Aug 28, 2012 at 11:11 AM
Did you make sure that the Marker prefab is inside a Resources folder?
Actually i figured it out a couple $$anonymous$$utes later and i answered my question but i can not see the answer yet..
That was the problem..I had placed the prefab in a folder called $$anonymous$$yPrefab. So when i renamed it to Resources the error gone!
Anyway, thank you and appreciated your effort! it is great to be in a community like this one.
Yes, you need 15 karma to have your answer posted without moderation. I will take a look at the moderation queue.
I'd give you a +1 but I don't have the karma. Just wanted to let you know that this helped me out.