- Home /
Create a TreePrototype causes a object null exception
Hi, well I'm creating a procedural world generation, and well I like to add some trees to my terrains (is in plurar because I'm using a terrain tile system)...
Well, the Docs and community hasn't enough information for me, so, I started to code some hours ago, and I can't resolve this problem...
My problem is that when I want to assing the Tree Prototype (this window) to my TerrainData object:
Using code, instead of using the window before, a null reference exception stops all the process... I don't know exactly why, because Docs doesn't say anything or how to assing TreePrototype to a TerrainData with code...
Anyway, there is the code that you can check and tell me what I'm doing wrong...
This code is for generate the terrainData:
public GameObject[] trees; //I used that because TreeProtype[] doesn't appear in Inspector...
TerrainData GenerateTile() {
TerrainData tData = new TerrainData();
//... Properties
tData.treePrototypes = GenerateTreeFromGOs(trees);
//... More properties
return tData;
}
Well the function GenerateTreeFromGOs is that (and this is where I have the problem):
TreePrototype[] GenerateTreeFromGOs(GameObject[] gos) {
TreePrototype[] trees = new TreePrototype[gos.Length];
for(int i = 0; i < trees.Length; i++) {
trees[i].bendFactor = m_bendFactor; //This part throw a null object exception
trees[i].prefab = gos[i]; //And this too...
}
return trees;
}
In this function I loop every assigned gameobject (via Inspector) and I add it to the prototypes...
So, how can I set the terrain Trees dynamically? Because, nobody (I think) has asked this before...
Thanks in advance.
Bye.
Answer by Loius · Jul 21, 2014 at 03:04 PM
This creates {gos.length} null references to treeprototypes:
TreePrototype[] trees = new TreePrototype[gos.Length];
You have to initialize them:
for(int i=0; i<gos.Length; i++) {
trees[i] = new TreePrototype();
}
I though that with the array that was enough :P I never get used to this... And this is a problem for me, because I never remember that...
Your answer
Follow this Question
Related Questions
Object-Spawning Script? 1 Answer
How to assign a gameObject to prefab (in-game via code)? 1 Answer
How do I Fade Terrain Tree's to Blend with Billboard? 1 Answer
How to assign a gameobject to a script that is added dynamically 1 Answer
Is there a way to dynamically populate game objects via texture maps? 2 Answers