• 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 determine 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers

set level to be played only 3 times 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges