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 /
  • Help Room /
avatar image
0
Question by 123mcole · Feb 25, 2016 at 10:33 AM · c#arrayout of range

Array Index Out of bounds, line error stated;

Line error is 197; I have no clue why, as i set there value up the first time, anything above 150 does not work, no clue?

using UnityEngine; using System.Collections;

public class GrassGaneration : MonoBehaviour {

 public GameObject[] blockidset = new GameObject[3];
 public Transform grasstrans;//-1 = air(nothing), 0 = grass, 1 = dirt, 2 = stone
 public Transform dirttrans;
 private int count;
 public int lastlongelevation;
 private int[] grassmap; //= new int[50];
 private int[] elevationmap; //= new int[50];
 private int[,] blockid; //=  new int [50,100];
 public int elevationnoise;
 public int elevationscalenoise;
 //private int randommineral;
 //private int oresizex;
 //private int oresizey;
 //private int[] orenoise = new int[40];
 //private int scaleore;
 //private int randomore2;
 //private int orechange;
 private int numbercount = 1;
 private int max;
 public int maplength;

 void Start()
 {

     grassmap = new int[maplength];
     elevationmap = new int[maplength];
     blockid = new int[maplength, 100];

     setblockidtozero();

     grassmapcreate();
     grassmapsmooth();

     creatorelevationmap();

     grassmapelevationmapcombine();

     findmaxgrassmapvalue();

     setblockid();

     createblocks();


 }



 // Update is called once per frame
 void Update()
 {

 }

 void creatorelevationmap() {

     int chanceofchange = 0;
     int scale = 0;
     int updown = 0;
     int length = 0;

     for (int q = 0; q < maplength; q++)
     {
         
         if (chanceofchange == 1)
         {
             length = (int)Mathf.Round(Random.Range(Mathf.Round(lastlongelevation/2), lastlongelevation));
             scale = (int)Mathf.Round(Random.Range(0, elevationscalenoise));
             updown = (int)Mathf.Round(Random.Range(0, 2)) - 1;
             chanceofchange = 0;
         }

         if (length == 0)
         {
             chanceofchange = (int)Mathf.Round(Random.Range(0, elevationnoise));
             scale = 0;
         }
         else if (length != 0)
         {
             scale = (int)Mathf.Round(Random.Range(0, elevationscalenoise));
             length = length - 1;
         }

         
         
         

         
         if (q == 0) {
             if (updown == 0)
             {
                 elevationmap[q] = scale;
             }
             else {
                 elevationmap[q] = -scale;
             }
         
     } else {
             if (updown == 0)
             {
                 elevationmap[q] = elevationmap[q - 1] + scale;
         }
             else {
                 elevationmap[q] = elevationmap[q - 1] - scale;
             }
         }

         if (elevationmap[q] + grassmap[q] < 19)
         {
             updown = 0;
         }

     }
 }
 void setblockidtozero()
 {
     for (int bb = 0; bb < maplength; bb++)
     {
         for (int bv = 0; bv < 100; bv++)
         {
             blockid[bb, bv] = -1;
         }
     }
 }
 void grassmapcreate()
 {
     grassmap[0] = (int)Mathf.Round(Random.Range(0, 3)) - 1;
     for (int c = 1; c < maplength; c++)
     {
         numbercount++;
         count = (int)Mathf.Round(Random.Range(0, 3));
         if (grassmap[c - 1] == -4)
         {
             grassmap[c] = grassmap[c - 1] + count;
         }
         else if (grassmap[c - 1] == 4)
         {
             grassmap[c] = grassmap[c - 1] + count - 2;
         }
         else {
             grassmap[c] = grassmap[c - 1] + count - 1;
         }

     }
 }

 void grassmapsmooth()
 {
     for (int u = 1; u < maplength - 1; u++)
     {
         if (grassmap[u - 1] == grassmap[u + 1])
         {
             if (grassmap[u] + 1 == grassmap[u - 1])
             {
                 grassmap[u] = grassmap[u - 1];
             }
             else if (grassmap[u] - 1 == grassmap[u - 1])
             {
                 grassmap[u] = grassmap[u - 1];
             }
         }
     }
 }

 void grassmapelevationmapcombine()
 {
     for (int y = 0; y < maplength; y++)
     {
         grassmap[y] = grassmap[y] + elevationmap[y];
     }
 }

 void findmaxgrassmapvalue()
 {
     max = grassmap[0];
     for (int p = 1; p < grassmap.Length; p++)
     {

         if (grassmap[p] > grassmap[p - 1])
         {
             max = grassmap[p];
         }

     }
 }

 void setblockid()
 {
     for (int b = 0; b < maplength; b++)
     {
         blockid[b, grassmap[b] + 60] = 0;

         for (int n = 0; n < (grassmap[b] + 60); n++)
         {
             if (n > 40)
             {
                 blockid[b, n] = 1;

             }
             else {
                 blockid[b, n] = 2;

             }

         }
     }
 }

 void createblocks()
 {
     for (int b1 = 0; b1 < maplength; b1++)
     {
         for (int n1 = 0; n1 < 100; n1++)
         {
             if (blockid[b1, n1] == -1)
             {

             }
             else {
                 Instantiate(blockidset[blockid[b1, n1]], new Vector2(8 * b1, (n1 - 60) * 8), dirttrans.rotation);
             }

         }
     }
 }

 

}

put this into your code editor to see the line numbers, i couldn't get "printscreen" to work on here.

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 Salmjak · Feb 25, 2016 at 10:55 AM 0
Share
 for (int n = 0; n < (grassmap[b] + 60); n++)
          {
              if (n > 40)
          {
              blockid[b, n] = 1;
          }

In the beginning you say blockid = new int[maplength, 100];

when n is n is above 100 you're trying to access an element that doesn't exist. It's out of the range of the array. So whenever grassmap[b] is above 40 you will try to access n > 100 and your array isn't that big.

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

Array index out of range. But it doesn't appear to be. 2 Answers

How to get all children of a Gameobject with a certain component 2 Answers

How to instantiate bullets in multiple transforms 1 Answer

Change value of array on start 1 Answer

How do I use an entire array at once? 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