- Home /
Custom class that contains prefabs as members
I'm trying to write my own class "BlockUnit". The class represents one "piece" of the game map, each piece should contain a variety of things such as pick ups that spawn as well as obstacles. Most importantly, it should contain a floor, which I have laid out as 3x3 planes attached to the same empty gameobject, which I named "Block" (I really should have named it Floor or something, but ah well). I want to have a block manager that deletes Blocks when appropriate and handles the list of blocks. However, my problem now is that I can't get the Block class to instantiate the Floor prefab.
This is my constructor:
public BlockUnit() {
unitQuaternion.Set (0, 0, 0, 1);
baseBlock = (GameObject) Instantiate (baseBlock, Vector3.zero, unitQuaternion);
baseBlock.gameObject.SetActive (false); // turn off the game object so it's invisible until needed
}
The game runs when I click play, but it gives me an error right here saying the prefab I am trying to instantiate is null. I for sure dragged the Block prefab into the baseBlock field in the editor, so I'm not sure what is going wrong here.
If you go in your Block file, you can right click on the class name, select Refactor, then Rename. You can now name your class FLoor and it will be changed wherever you referenced it.
Refactor magic!
Your answer
Follow this Question
Related Questions
How to Instantiate Custom Class 2 Answers
Updating a variable on a script in an instanced object 1 Answer
Instantiate object 1 Answer