- Home /
Need help with assigning game objects to variable
made an array of a custom class and i want to assign a prefab to class variable so assigning from another(inspector exposed) variable. relvent Code:
public class ChunkData {
public string BiomeName ="Forest";
public GameObject landType =null;
public GameObject[] NatureTypes =null;//list of nature placeables
public GameObject[] ObjList =null;//list if generated objects
public Object[] LoadedList;
public float worldX =0;//location in world in unity units
public float worldZ =0;//location in world in unity units
public int objDensity =100;
}
public class LandGenerator3 : MonoBehaviour {
public GameObject LandMesh =null;//land object(for now)
void Start () {
ChunkList = new ChunkData[worldSizeX, worldSizeZ];
void FillChunks(){
for (int x=0; x < worldSizeX; x++) {
for (int z=0; z < worldSizeZ; z++) {
ChunkList [x, z].landType = LandMesh ;**<<where i get error**
ChunkList [x, z].NatureTypes = NatureList;
ChunkList [x, z].objDensity = objDensity;
ChunkList [x, z].worldX = x * 50;
ChunkList [x, z].worldZ = z * 50;
CreateChunk(ChunkList[x,z],x,z);
print ("Chunk Filled?");
}
}
}
this is the error i get btw: NullReferenceException: Object reference not set to an instance of an object LandGenerator3.FillChunks () (at Assets/LandGenerator3.cs:38) LandGenerator3.Start () (at Assets/LandGenerator3.cs:20)
sorry maybe i wasnt clear: public GameObject Land$$anonymous$$esh =null; < is assigned prefeab in editor
ChunkList [x, z].landType = Land$$anonymous$$esh ; is where im having my problem this is in same script as Land$$anonymous$$esh.
Chunklist is an array of type ChunkData which is defined in a script solely as a Class type not attached to any game
Answer by Yusaku · Jan 20, 2014 at 07:30 PM
Figure it out( a way anyway) you have to "reserve memory" for your data from your class, at least arrays and the like. for example:
void InitChunks(){
for (int x=0; x < worldSizeX; x++) {
for (int z=0; z < worldSizeZ; z++) {
ChunkList[x,z] = new ChunkData();
ChunkList[x,z].landType = new GameObject("land");
ChunkList[x,z].NatureTypes = new GameObject[10];
ChunkList[x,z].ObjList = new GameObject[objDensity];
ChunkList[x,z].LoadedList = new Object[objDensity];
ChunkList[x,z].ObjLoc = new Vector3[objDensity];
}
}
}
Answer by getyour411 · Jan 19, 2014 at 07:49 PM
public GameObject myPrefab;
Drag/Drop prefab into it in Editor, or assign GObj reference in another script to thisScript.myPrefab