- Home /
Solution to MissingReferenceException w/ prefab?
alright I've rewritten the entire question because it was rather unclear before;
What I am trying to do is instantiate a prefab that exists as a variable in another script.
class myClass { //Just a class to represent the
var item : GameObject; //class I have in my script
}
var a_Class : myClass = myClass();
function create () { //Instantiate the gameobject from "item"
Instantiate(a_Class.item,//position,//rotation);
}
The only issue with this script is that the item variable comes from a gameObject that has been destroyed...so when i try to instantiate it over again, i receive a "GameObject you are trying to access has been destroyed" error because the prefab from which i got the item variable has been removed from the game world.
what i need is a way to retain this prefab because the prefab that i am adding and removing from an array i have in the script is the same and has itself assigned to "item".
You should edit your question and post the relevant parts of your code - it's hard to understand what's going wrong solely based on your description.
Answer by appearance · Jan 30, 2013 at 08:36 AM
Your question is not clear. I agree on aldonaletto's comment.
I think you are getting problem to instantiate a prefab at runtime. Seems prefab reference problem.
You can create a 'Resources' folder in Assets folder of your project and move your prefab(s) to 'Resources' folder. Now, you can instantiate your prefab(s) run-time like this:
Instantiate (Resources.Load ("myPrefab", typeof(GameObject)));
Make sure 'myPrefab.prefab' file exists in 'Assets/Resources' folder, or else you will get a NullReference Exception.
thanks, i think Resources.Load should do the trick, that seems to be what I was looking for. As for the question, i'll rewrite it so it's more helpful to everyone looking at this question