- Home /
JS: How to instantiate a prefab from a public class?
Hey all,
I've spent most of the day converting my "equipped weapons" system into a public class.
I have a function in it that's called every time the Inventory screen is closed:
function InstantiateAllWeps(GV : GlobalVars) { Debug.Log("Attempting to instantiate weapons"); var gg : GameObject; var a : int; var git : ItemClass; for (a = 0; a < 4; a++) { git = item[a]; if (git.exist) { Debug.Log("Item " + git.ty + " exists!");
// The line below is the one with the error
gg = GameObject.Instantiate(GV.prefabGun[git.ty], Vector3.zero, Quaternion.identity);
}
}
}
The actual function is much more complicated than that, but this is the part I'm having trouble with. Unity gives me an error and says that the "Object reference is not set to an instance of an object."
I've tried making GV a variable in the class and setting it when I create this class... passing it as an argument (as you see in the code above)... Nothing works.
Also, I've confirmed via the inspector that yes, it DOES have a pointer to GlobalVars during runtime. Not sure why it can't access the variables.
Any help would be greatly appreciated, as I'm at my wit's end here. Thanks!
Answer by TerraMeliorRPG · Mar 25, 2013 at 02:18 AM
SOLUTION:
I had to go into the GlobalVars script and make prefabGun a public variable.
Your answer
Follow this Question
Related Questions
How can I declare class properties of an object in a single line? 2 Answers
Spawn along local axis. 3 Answers
instantiate object and align with object surface 2 Answers
What is the javascript equivalent of "using"? 1 Answer
Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer