Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 94931355 · Jul 18, 2016 at 04:56 AM · gameobjectarray3dupdatestart

New Object Instantiation from Array working in Start() but not Update()

Hello there! I'm fairly new to unity and C# in general, so I'm hoping one of you fine people can help me out. I'm trying to dynamically load chunks of map data as my player walks around. The initial load works in the start() function, but when I run the exact same function in update() as my player nears the edge of the map it throws this error:

 NullReferenceException: A null value was found where an object instance was required.
 World.NewChunk (Int32 x, Int32 y, Int32 z) (at Assets/World.cs:61)
 World.ChunkUpdate () (at Assets/World.cs:100)
 World.Update () (at Assets/World.cs:55)

The error points at my NewChunk() function, specifically the reference to "chunks[x,y,z]= Instantiate(chunk,". I'm very confused because from what I understand the Update() function seems to be unable to access the chunks array I've setup. Any idea why this is? I've included the full World script below.

 using UnityEngine;
 using System.Collections;
 using System.IO;
 using System.Runtime.Serialization;
 
 public class World : MonoBehaviour {
 
     public byte[,,] data;
     public int worldX=511;
     public int worldY=126;
     public int worldZ=510;
 
     public int cullDist = 4;
 
     public GameObject chunk;
     public GameObject[,,] chunks;
     public bool[,,] chunkTest;
     public int chunkSize=32;
 
     public int playerChunkX = -1;
     public int playerChunkz = -1;
 
     public int timerChunkUpdate = 5;
 
     void Start () {
 
         data = new byte[worldX,worldY,worldZ];
 
         TextAsset World = Resources.Load("NewPompeii") as TextAsset;
         MemoryStream s = new MemoryStream (World.bytes);
         var b = new BinaryReader(s);
 
         for (int y=0; y<worldY; y++){
             for (int z=0; z<worldZ; z++){
                 for (int x=0; x<worldX; x++) {
                     data[x,y,z] = b.ReadByte();
                 }
             }
         }
 
         chunks=new GameObject[Mathf.FloorToInt(worldX/chunkSize),
             Mathf.FloorToInt(worldY/chunkSize),
             Mathf.FloorToInt(worldZ/chunkSize)];             
 
         ChunkUpdate ();
 
     }
 
     void Awake() {
         //Application.targetFrameRate = 30;
     }
 
     void Update () {
 
         ChunkUpdate ();
             
     }
 
     void NewChunk(int x, int y, int z){
 
         chunks[x,y,z]= Instantiate(chunk,
             new Vector3((x)*chunkSize,(y)*chunkSize,(z)*chunkSize),
             new Quaternion(0,0,0,0)) as GameObject;
 
         Chunk newChunkScript = chunks[x,y,z].GetComponent("Chunk") as Chunk;
 
         newChunkScript.name = "chunk " + x + "," + y + "," + z;
         newChunkScript.worldGO=gameObject;
         newChunkScript.chunkSize=chunkSize;
         newChunkScript.chunkX=(x)*chunkSize;
         newChunkScript.chunkY=(y)*chunkSize;
         newChunkScript.chunkZ=(z)*chunkSize;
         //newChunkScript.cullDist=cullDist;
         //newChunkScript.rend = newChunkScript.GetComponent<Renderer> ();
 
         //chunkTest [x, y, z] = true;
 
         return;
 
     }
 
     void ChunkUpdate(){
 
         GameObject player = GameObject.FindWithTag("MainCamera");
         Transform target = player.transform;
         var targetX = (int) target.position.x/chunkSize;
         //var targetY = (int) target.position.y/chunkSize;
         var targetZ = (int) target.position.z/chunkSize;
 
         if (targetX != playerChunkX || targetZ != playerChunkz) {
             playerChunkX = targetX;
             playerChunkz = targetZ;
 
             for (int y = 0; y < worldY/chunkSize; y++) {
                 for (int z = 0; z < 2*cullDist; z++) {
                     for (int x = 0; x < 2*cullDist; x++) {
                         if (GameObject.Find ("chunk " + (targetX + x-cullDist) + "," + y + "," + (targetZ + z-cullDist))) {
                         //if (chunkTest[targetX + x - cullDist, y, targetZ + z - cullDist]){
                         } else if (targetX+x-cullDist < Mathf.FloorToInt(worldX/chunkSize) && targetZ+z-cullDist < Mathf.FloorToInt(worldZ/chunkSize)){
                             NewChunk (targetX+x-cullDist, y, targetZ+z-cullDist);
                         }
                     }
                 }
             }
         }
     }
 
 
     public byte Block(int x, int y, int z){
 
         if( x>=worldX || x<0 || y>=worldY || y<0 || z>=worldZ || z<0){
             return (byte) 1;
         }
 
         return data[x,y,z];
     }
 
 }



Comment
Add comment · Show 1
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 IvovdMarel · Jul 18, 2016 at 05:40 AM 0
Share

The best way to debug this is by using a breakpoint and attach $$anonymous$$onoDevelop / VS to Unity. If you are not sure how to do this, I recommend looking it up.

$$anonymous$$y first guess would be that it's chunk that becomes null. Are you sure that it's pointing to a prefab, not an object in your scene that maybe gets destroyed?

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make an array of all visible objects 1 Answer

How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers

Checking if a position is occupied in 2D? 1 Answer

Update character's position after animation 1 Answer

2D Array of GameObjects... 1 Answer


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