Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by z3nth10n · Jul 20, 2014 at 02:50 PM · gameobjecttreesdynamically

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:

Trees

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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();
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image z3nth10n · Jul 21, 2014 at 03:09 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges