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 SoraMahiro · Dec 18, 2016 at 11:02 PM · c#array-out-of-range-excepttetris

Array out of index, but shouldn't be

I have a grid that is 15 wide and 20 high, I also have a method in which checks and updates this grid when something comes into it and that object becomes apart of the grid, but the moment the object is instantiated the grid array becomes out of index... I've looked over it and it shouldn't, but as usual I'm most likely not seeing something. Here's the code:

 using System;
 using UnityEngine;
 using System.Collections;
 
 namespace GameManager
 {
     public class GameManager:MonoBehaviour
     {
         Instantiator instance;
         public static int gridWidth = 15;
         public static int gridHeight = 21;
         private static Transform[,] gridMatrix;
 
         void Start()
         {
             instance = FindObjectOfType<Instantiator>();
             gridMatrix = new Transform[gridWidth, gridHeight];
             instance.Spawn();
         }
         public void UpdateGrid(Tetro tetro)
         {
             for (int y = 0; y < gridHeight; y++)
             {
                 for (int x = 0; x < gridWidth; x++)
                 {
                     if (gridMatrix[x, y] != null)
                     {
                         if (gridMatrix[x, y].parent == tetro.transform)
                         {
                             gridMatrix[x, y] = null;
                         }
                     }
                 }
             }
             foreach (Transform mino in tetro.transform)
             {
                 Vector2 pos = RoundOffPositions(mino.position);
                 if (pos.y < gridHeight)
                 {
                     gridMatrix[(int)pos.x, (int)pos.y] = mino;
                 }
             }
         }
         public Transform getChildPositionInGrid(Vector2 pos)
         {
             if (pos.y > gridHeight - 1)
             {
                 return null;
             }
             else
             {
                 return gridMatrix[(int)pos.x, (int)pos.y];
             }
         }
         public bool CheckIsInGrid(Vector2 pos)
         {
             return (pos.x >= -7 && pos.x <= 7 && pos.y >= -10);
         }
         public Vector2 RoundOffPositions(Vector2 pos)
         {
             return new Vector2(Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y));
         }
         void Update()
         {
             
         }
     }
 }

and:

 using System;
 using UnityEngine;
 using System.Collections;
 
 public class Tetro:MonoBehaviour
 {
     Instantiator instance;
     GameManager.GameManager management;
     public bool hasLimitedRotation;
     
     void Start ()
     {
         instance = FindObjectOfType<Instantiator>();
         management = FindObjectOfType<GameManager.GameManager>();
         StartCoroutine(Fall());
     }
     IEnumerator Fall()
     {
         foreach(Transform mino in transform)
         {
             while (mino.position.y > -10)
             {
                 yield return new WaitForSeconds(1);
                 transform.position -= new Vector3(0, 1);
                 if (!checkEachChildPosition())
                 {
                     transform.position += new Vector3(0, 1);
                     instance.Spawn();
                     enabled = false;
                     yield break;
                 }
             }
         }
     }
     public void CheckInput()
     {
         if (transform.position.y > -9)
         {
             if (Input.GetKeyDown(KeyCode.RightArrow))
             {
                 transform.position += new Vector3(1, 0);
                 if (!checkEachChildPosition())
                 {
                     transform.position += new Vector3(-1, 0);
                 }
                 else
                 {
                     management.UpdateGrid(this);
                 }
             }
             else if (Input.GetKeyDown(KeyCode.LeftArrow))
             {
                 transform.position += new Vector3(-1, 0);
                 if (!checkEachChildPosition())
                 {
                     transform.position += new Vector3(1, 0);
                 }
                 else
                 {
                     management.UpdateGrid(this);
                 }
             }
             else if (Input.GetKeyDown(KeyCode.UpArrow))
             {
                 transform.Rotate(0, 0, 180);
                 if (hasLimitedRotation)
                 {
                     transform.Rotate(0, 0, -90);
                 }
             }
             else if (Input.GetKeyDown(KeyCode.DownArrow))
             {
                 transform.position -= new Vector3(0, 1);
                 if (!checkEachChildPosition())
                 {
                     transform.position += new Vector3(0, 1);
                 }
                 else
                 {
                     management.UpdateGrid(this);
                 }
             }
         }
     }
     bool checkEachChildPosition()
     {
         foreach (Transform mino in transform)
         {
             Vector2 pos = management.RoundOffPositions(mino.position);
             if (management.CheckIsInGrid(pos) == false)
             {
                 return false;
             }
             if (management.getChildPositionInGrid(pos) != null && management.getChildPositionInGrid(pos).parent != transform)
             {
                 return false;
             }
         }
         return true;
     }
     void Update ()
     {
         CheckInput();
     }
 }

this is the error on the stack:

 IndexOutOfRangeException: Array index is out of range.
 GameManager.GameManager.getChildPositionInGrid (Vector2 pos) (at Assets/Scripts/GameManagers/GameManager.cs:52)
 Tetro.checkEachChildPosition () (at Assets/Scripts/Tetros/Tetro.cs:94)
 Tetro+<Fall>c__Iterator0.MoveNext () (at Assets/Scripts/Tetros/Tetro.cs:25)
 UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
 
Comment
Add comment · Show 4
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 SoraMahiro · Dec 19, 2016 at 12:14 AM 0
Share

Note: The gridHeight is 21 because I was checking the values to make sure that it wasn't a problem with the values. The same problem occurred at the various values.

avatar image SoraMahiro · Dec 19, 2016 at 05:48 PM 0
Share

Help, anybody???

avatar image TreyH · Dec 19, 2016 at 06:05 PM 0
Share

You don't have copy-able code here and don't seem to be making any sort of print statements / etc to help debug your situation.

You're giving indices based on a rounded float, which is weird. The simple check would be to swap those two and see if you're just backwards on your x,y.

avatar image SoraMahiro TreyH · Dec 19, 2016 at 06:34 PM 0
Share

Okay, I tried switching them, got the same error. I ran some debugging logs to trace the problem. But to no avail. I still got the same problem. I took out the Round() method because I saw what you meant, it was unnecessary, but that didn't change a thing either. I'm assu$$anonymous$$g that I may not have the Game$$anonymous$$anager on the right object for it to read the grid properly, so I'm gonna try that and see what happens.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SoraMahiro · Dec 19, 2016 at 11:51 PM

Found the first problem, at line 57 of GameManager, the correct return is supposed to be return (pos.x >= 0 && pos.x < gridWidth && pos.y >= 0) , however now I have the issue that after the first object falls in the grid and lands, the grid updates but the next objects that spawn all get stuck at the top and they continue to spawn. The pieces do not fall. I've never had this much trouble making a game before.

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

Answer by dpoly · Dec 20, 2016 at 05:41 AM

There are several possibilities here. Do not thrash around trying random fixes -- diagnosis before treatment.

First, find out the exact values that are 'out of range', using Debug.Trace() or the debugger;

Second, make sure that grid width and height are what you expect -- public variables can be changed.

Be particularly careful about mixing ints and floats: best to case to int before checking in range.

Check both height and width, not just height.

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

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

Related Questions

Can anyone tell me how to store a block in a grid of a Tetris game . Help 0 Answers

Unexplainable NullReferenceException? 1 Answer

How to solve "Array Index is out of range" 1 Answer

Array index is out of range? 4 Answers

IndexOutOfRangeException: Array index is out of range. WeaponManager.Start () (at Assets/Resources/NewScripts/WeaponManager.cs:55) 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