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
0
Question by Gabriel Rausch · May 30, 2013 at 04:50 AM · c#array

Problem with World Generate

hello guys, tou have a problem that when the world is generated it does not generate all and this error "the thing you want to instantiate is null" Anyone know where should be the error?

 using UnityEngine;
 using System.Collections;
 
 public class WorldGenerate : MonoBehaviour {
     
     public int BlockSize = 1;
     public int WorldWidth = 100;
     public int WorldHeight = 20;
     
     public int topOfTheWorld = 20;
     public int yPos = 20;
     
     public int slope = 0;
     
     public int xPos = 0;
     
     public int contBlocksInt = 0;
     
     private GameObject [][] scene;
     
     private string [] biomes;
     public int TimeUntilNextBiome;
     public string biome;
     
     private string [] caves;
     public int TimeUntilNextCave;
     public string cave;
     
     public GameObject World;
     
     public Vector3 PositionBlock;
     
     public GameObject air;
     public GameObject stone;
     public GameObject dirt;
     public GameObject grass;
     public GameObject coal;
     public GameObject wood;
     public GameObject leaves;
     public GameObject sand;
     public GameObject bedrock;
     
     void Start () {
         
         scene = new GameObject[100000][];
         
         biomes = new string[10];
         
         yPos = topOfTheWorld;
         
         this.generate();
         this.render();
         
     }
     
     void Update () {
     
         
         
     }
     
     public void generate(){
         
         bool finished = false;
         
         biomes[0] = "forest";
         biomes[1] = "desert";
         
         biome = biomes[Random.Range(0,2)];
         
         caves[0] = "small";
         caves[1] = "medium";
         caves[2] = "large";
         
         cave = biomes[Random.Range(0,3)];
         
         TimeUntilNextBiome = Random.Range(0, 10) + 30;
         TimeUntilNextCave = Random.Range(0, 30) + 30;
         
         while(!finished){
             
             if(scene[xPos] == null){
         
                 scene[xPos] = new GameObject[100000];
             
             }
             
             if(yPos == topOfTheWorld){
                 
                 TimeUntilNextBiome--;
                 TimeUntilNextCave--;
                 
                 if(TimeUntilNextBiome <= 0){
                 
                     biome = biomes[Random.Range(0,2)];
                     TimeUntilNextBiome = Random.Range(0, 10) + 30;
                     
                 }
             
                 if(TimeUntilNextCave <= 0){
                 
                     cave = caves[Random.Range(0,3)];
                     TimeUntilNextCave = Random.Range(0, 30) + 30;
                     
                 }
                 
                 if(biome == "forest"){
                     
                     scene[xPos][yPos] = grass;
                     
                 if(Random.Range(0, 6) == 1){
                     
                     scene[xPos][yPos + 1] = wood;
                     scene[xPos][yPos + 2] = wood;
                     scene[xPos][yPos + 3] = wood;
                     
                     if(scene[xPos + 1] == null){
         
                         scene[xPos + 1] = new GameObject[10000];
             
                     }
                     
                     scene[xPos - 1][yPos + 3] = leaves;
                     scene[xPos + 1][yPos + 3] = leaves;
                     scene[xPos][yPos + 4] = leaves;
                     
                 }
                 
                 }else if(biome == "desert"){
                 
                     scene[xPos][yPos] = sand;
                     
                 }
                     
             }else if(yPos >= topOfTheWorld - 3 && Random.Range(0, 3) != 1){
                 
                 if(biome == "forest"){
                 
                     scene[xPos][yPos] = dirt;
                 
                 }else if(biome == "desert"){
                 
                         scene[xPos][yPos] = sand;
                     
                 }
                     
             }else if(yPos >= topOfTheWorld - 6 && Random.Range(0, 2) != 1){
                 
                 if(biome == "forest"){
                 
                     scene[xPos][yPos] = dirt;
                 
                 }else if(biome == "desert"){
                 
                         scene[xPos][yPos] = sand;
                     
                 }
                     
             }else{
                 
                 if(Random.Range(1, 100) == 1){
                 
                     scene[xPos][yPos] = coal;
                     
                 }else{
                 
                     scene[xPos][yPos] = stone;
                 
                 }
                     
             }
             
             yPos--;
             
             if(yPos < 0){
                 
                 slope = Random.Range(0, 5) - 2;
                 
                 if(topOfTheWorld < 10){
                 
                     slope = 1;
                     
                 }
                 
                 if(topOfTheWorld > 30){
                 
                     slope = -1;
                     
                 }
                 
                 topOfTheWorld += slope;
                 yPos = topOfTheWorld;
                 
                 xPos++;
                 
             }
             
             if(xPos > WorldWidth){
                 
                 for(var i = 0; i <= WorldWidth; i++){
                 
                     scene[i][1] = bedrock;
                     
                 }
                 
                 for(var i = 0; i <= 50; i++){
                 
                     scene[1][i] = bedrock;
                     
                 }
                 
                 for(var i = 0; i <= 50; i++){
                 
                     scene[WorldWidth][i] = bedrock;
                     
                 }
                 
                 finished = true;
             
             }
             
         }
         
     }
     
     public void render(){
     
         for(var x = 1; x <= WorldWidth; x++){
     
             for(var y = 1; y <= WorldHeight; y++){
         
                 GameObject World = (GameObject)Instantiate(scene[x][y]) as GameObject;
                 World.transform.position = new Vector3(x * BlockSize - BlockSize / 2, y * BlockSize + BlockSize / 2, 0);
                 World.name = "bX" + World.transform.position.x+"bY" + World.transform.position.y;
                 
             }
     
         }
         
     }
     
 }
 
Comment
Add comment · Show 2
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 moghes · May 30, 2013 at 10:29 AM 0
Share

check robertbu's answer, you are forgetting to drag you prefabs to the proper variables.

avatar image z3nth10n · Nov 28, 2013 at 07:30 PM 0
Share

Sorry for revive, but I have to say that, you have copy Zanzlanz's tutorials!! LOL :P

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · May 30, 2013 at 04:53 AM

Is this your script or is it one you downloaded from somewhere? If the latter, you should make reference to the original author or location.

As for the error message, this would happen if any of the game objects on lines 33 - 41 (air, stone, dirt...) are not initialized. This script expects these game objects to be initialized by dragging a prefab for each of these game objects onto these variables in the Inspector.

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 Gabriel Rausch · May 30, 2013 at 08:39 PM -1
Share

all prefabs are already stored in espector.

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

15 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

Related Questions

Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers

[SOLVED] Problem with "foreach". 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make an array list set to false gradually instead of instantly? 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