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 Theacesofspades · Dec 15, 2014 at 06:14 PM · c#arrayloopnull reference exception

I dont know why im getting a null reference exception

My first script makes an array and fills it with objects:

 IEnumerator CreateTerrain () 
     {
         for (int px=0; px<chunkSize; px++)
         {
             for (int pz=0; pz<chunkSize; pz++)
             {
                 float Perlin1 = Mathf.PerlinNoise((px+ transform.position.x) /xScale, heightScale);
                 float Perlin2 = Mathf.PerlinNoise((pz+ transform.position.z) /xScale, heightScale);
                 
                 int x = (int) Mathf.FloorToInt(px + transform.position.x);
                 int height = (int) Mathf.FloorToInt(Perlin1*40*Perlin2);
                 int z = (int) Mathf.FloorToInt(pz + transform.position.z);
                 
                 for (int y=0; y<height; y++)
                 {
                     yield return new WaitForSeconds (1/blocksPerSecond);
                     GameObject currentBlock = GameObject.Instantiate(blocks[0], new Vector3(x, y, z), Quaternion.identity) as GameObject;
                     currentBlock.transform.parent = transform;
                     levelData[x, y, z] = currentBlock;
                 }
             }
         }
         
         foreach (Transform block in transform)
         {
             block.GetComponent<BlockScript>().UpdateSides();
         }
     }


My Script that im getting the error on is:

 public void UpdateSides () 
     {
         levelData = new GameObject[chunkSize, chunkHeight, chunkSize];
         
         levelData = leveldataScript.levelData; *** null reference exception ***
         
         print (levelData.GetLength(0) + " , " + levelData.GetLength(1) + " , " + levelData.GetLength(2));
         
         int x = (int) transform.position.x;
         int y = (int) transform.position.y;
         int z = (int) transform.position.z;
         // it continues.....
 

iv been trying to figure out why im getting the error but i have no idea. The console prints out: 10, 100, 10 which means that the array isnt null. Can anyone help please thank you

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 spiceboy9994 · Dec 15, 2014 at 06:40 PM 0
Share

In your code... what is leveldataScript?

levelData = leveldataScript.levelData

The console is printing out values because you have a levelData variable instantiated before being used. I cannot tell if that is public global variable, since I don't see the whole class code. If you could post the definition for both scripts, would be easier to deter$$anonymous$$e the error. $$anonymous$$y gut's tell me that leveldataScript is never initialized.

avatar image richyrich · Dec 15, 2014 at 07:55 PM 0
Share

On which line does the error appear - it should say on the console window, on the error message

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Dec 15, 2014 at 07:07 PM

Probably because you have a null reference. The error means "something before a dot is null". So go to the error line. Find all the dots. And check everything immediately before a dot has been assigned a value.

Comment
Add comment · 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

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

set level to be played only 3 times 1 Answer

How to deactivate all GameObject in a array, except last one 4 Answers

OverlapSphere for parallel arrays 1 Answer

Checking an array variable in C# vs JavaScript 3 Answers

How to create an image gallery with previous and next button with C#? 6 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