Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ysj970828 · Nov 02, 2020 at 10:04 AM · instantiatedestroyindex

Tetris game - Destroy game object with tag doesn't work and Index is out of bounds of array,Tetris game -Destroy game object with tag doesn't destroy and Index is out of bounds of array

I started learning Unity & C# and decided to develop a Tetris game as a practice.

I have most functions working except Instantiating preview pieces which are shown in the right side of the screen.

I have my codes separated into two: one for Tetris block spawner and one for Tetris block. Tetris block code is applied to each block.

I have a few problems right now.

  1. (Whenever a Tetris block is placed, the first block in the preview is then played but the previews that have been instantiated before do not get destroyed, resulting in previews being instantiated on top of one another.) Edit : This is fixed

  2. (I also get the "Index was outside the bounds of the array" error although I don't see why index goes out of bounds.) Edit : This is fixed

  3. The "DeleteLine" function does not work now. It was working fine before I added preview of Tetris blocks.

I'm sorry if I'm asking a lot of question at once. I appreciate any help or hints.

Spawner Code:

 public List<GameObject> Sevenbag;
 public GameObject[] Baglist; // this is where i store prefabs
 private int randomizer;
 public GameObject[] CopyPrefab;

 
 private static Vector2[] ColOneNext = new[] { new Vector2(13f, 16f),
                                                   new Vector2(13f, 12.5f),
                                                   new Vector2(13f, 9f),
                                                   new Vector2(13f, 5.5f),
                                                   new Vector2(13f, 2f) };
 
   
 public GameObject CurrentTetrimino;
 public bool PreviewUpdate = true;
 
     // Start is called before the first frame update
     void Start()
     {
         BagRandomizer();
         NewTetrimino();
     }
 
     // Update is called once per frame
     void Update()
     {
 
         PreviewInitializer();
         BagRandomizer();
     }
 
 
     public void NewTetrimino()
     {
         CurrentTetrimino = Instantiate(Sevenbag[0], transform.position, Quaternion.identity);
         CurrentTetrimino.GetComponent<TetrisBlock>().enabled = true;
         Sevenbag.RemoveAt(0);
         Destroy(GameObject.FindWithTag("PreviewTetrimino"));
         PreviewUpdate = true;
     }
 
 
     public void PreviewInitializer()
     {
         if (Sevenbag.Count() > 10)
         {
             CopyPrefab = new GameObject[Sevenbag.Count()];
             for (int i = 0; i <= 5; i++)
             {
                 CopyPrefab[i] = Instantiate(Sevenbag[i], ColOneNext[i], Quaternion.identity) as GameObject; //this is where I get the Index outside the bounds of the array error
                 CopyPrefab[i].GetComponent<TetrisBlock>().enabled = false;
                 CopyPrefab[i].tag = "PreviewTetrimino";
                 PreviewUpdate = false;

             }
 
         }
 
         else
         {
             BagRandomizer();
         }
     }
    
     public void BagRandomizer()
     {
         while (Sevenbag.Count() <= 14)
         {
             List<int> randomint = new List<int>();
             int randomizer;
             for (int i = 0; i < 7; i++)
             {
                 do
                 {
                     System.Random r = new System.Random();
                     randomizer = r.Next(0, 7);
                 } while (randomint.Contains(randomizer));
                 randomint.Add(randomizer);
                 Sevenbag.Add(Baglist[randomint[i]]);
                 Console.Write(Sevenbag[i]);
             }
         }
 
     }





Tetris Block Code:

 public static int height = 20;
     public static int width = 10;
 
     public static float fallTime = 0.8f;
     private float previousTime;
 
     private static Transform[,] grid = new Transform[width, height];
 
     public Vector3 rotationPosition;
 
     public float das_rate = 0.1f;
     public float das_check = 0;
 
     private bool CheckHold = false;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {

 //codes of control of tetris blocks are deleted because they don't have any bug.
 
     }
 
 
     bool ValidMove()
     {
         foreach (Transform children in transform)
         {
             int roundedX = Mathf.RoundToInt(children.transform.position.x);
             int roundedY = Mathf.RoundToInt(children.transform.position.y);
 
             if (roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height)
             {
                 return false;
             }
 
             if (grid[roundedX, roundedY] != null)
             {
                 return false;
             }
         }
 
         return true;
     }
 
 
     void AddToGrid()
     {
         foreach (Transform children in transform)
         {
             int roundedX = Mathf.RoundToInt(children.transform.position.x);
             int roundedY = Mathf.RoundToInt(children.transform.position.y);
 
             grid[roundedX, roundedY] = children;
         }
     }
 
 
 
 
     bool HasLine(int i)
     {
         for (int j = 0; j < width; j++)
         {
             if (grid[j, i] == null)
                 return false;
         }
 
         return true;
     }
 
     void DeleteLine(int i)
     {
         for (int j = 0; j < width; j++)
         {
             Destroy(grid[j, i].gameObject);
             grid[j, i] = null;
         }
     }
 
     void RowDown(int i)
     {
         for (int y = i; y < height; y++)
         {
             for (int j = 0; j < width; j++)
             {
                 if (grid[j, y] != null)
                 {
                     grid[j, y - 1] = grid[j, y];
                     grid[j, y] = null;
                     grid[j, y - 1].transform.position += new Vector3(0, -1, 0);
                 }
             }
         }
     }
 
 
     void CheckForLines()
     {
         for (int i = height; i >= 0; i--)
         {
             if (HasLine(i))
             {
                 DeleteLine(i);
                 RowDown(i);
             }
         }
     }








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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Nov 02, 2020 at 10:51 AM

In your for loop

 for (int i = 0; i <= 5; i++)
              {
                  CopyPrefab[i] = Instantiate(Sevenbag[i], ColOneNext[i], Quaternion.identity) as GameObject; //this is where I get the Index outside the bounds of the array error
                  CopyPrefab[i].GetComponent<TetrisBlock>().enabled = false;
                  CopyPrefab[i].tag = "PreviewTetrimino";
                  PreviewUpdate = false;
              }

You set i to be less than or equal to 5. This means that i can be 0, 1, 2, 3, 4, and 5. The value ColOneNext[5] does not exist since the array only consists of 5 elements and starts at the 0th index, meaning that ColOneNext[5] is referring to the 6th element in the ColOneNext array, which does not exist, and is therefore returning the Index is out of bounds error.

To fix this, change the line for (int i = 0; i <= 5; i++) to for (int i = 0; i < 5; i++)

@ysj970828

Comment
Add comment · Show 1 · 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 ysj970828 · Nov 02, 2020 at 11:48 AM 0
Share

wow I don't know how I missed such simple mistake. Thank you very much for pointing that out. That error is gone now thanks to you.

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

175 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

Related Questions

how to make a Portal 2-esque laser beam "redirection" system? 0 Answers

Destroying all enemy characters after the level is over 4 Answers

how to destroy an object then instantiate another from a prefab 1 Answer

Instantiating droppable items on destroy game object 1 Answer

Raycast Specific Object/Instantiate Explosion 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