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 /
avatar image
0
Question by Dinosaurstic · Sep 30, 2017 at 03:22 AM · listsvector2for loopinstantiating

For loops / lists / instantiating not working! D:

I've been having trouble with instantiating and for loops.

I have been working on a game where the player puts in coordinates for a grid and game objects are instantiated around the lengths of the grid. I have a piece of code which is NOT working and I've been stuck on it for about a week now, around a for loop based on the count of a list. I'll show the code and arguments of it down below:

( The problem is with PlaceWall() and PlaceFloor() )

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FloorGenerator : MonoBehaviour {
 
     public GameObject floor;
     public GameObject wall;
     public GameObject landParent;
 
     List<Vector2> possibleFloorAreas = new List<Vector2>();
     public List<Vector2> possibleWallAreas = new List<Vector2>();
     List<GameObject> tileSets = new List<GameObject>();
 
     public void Start()
     {
         DiscoverAreas();
     }
 
     public void DiscoverAreas()
     {
         possibleFloorAreas.Clear();
 
         for (int r = 0; r <= CoinGenerator.instance.rows; r++)
         {
             for (int c = 0; c <= CoinGenerator.instance.collumns; c++)
             {
                 possibleFloorAreas.Add(new Vector2(c, r));
             }
         }
 
         possibleWallAreas.Clear();
 
         for (int u = -1; u <= CoinGenerator.instance.collumns + 1; u++)
         {
             possibleWallAreas.Add(new Vector2(u, -1));
             possibleWallAreas.Add(new Vector2(u, CoinGenerator.instance.rows + 1));
         }
 
         for(int o = 0; o <= CoinGenerator.instance.rows; o++)
         {
             possibleWallAreas.Add(new Vector2(-1, o));
             possibleWallAreas.Add(new Vector2(CoinGenerator.instance.collumns + 1, o));
         }
 
         PlaceFloor();
         PlaceWall();
     }
 
     public void PlaceFloor()
     {
         for(int h = 0; h <= possibleFloorAreas.Count; h++)
         {
                 GameObject theFloor = Instantiate(floor, possibleFloorAreas[h], Quaternion.identity);
                 tileSets.Add(theFloor);
                 theFloor.transform.SetParent(landParent.transform);
         }
     }
 
     public void PlaceWall()
     {
         for (int d = 0; d <= possibleWallAreas.Count; d++)
         {
             GameObject theWall = Instantiate(wall, possibleWallAreas[d], Quaternion.identity);
             tileSets.Add(theWall);
             theWall.transform.SetParent(landParent.transform);
         }
     }
     void DestroyLand()
     {
         foreach (GameObject tile in tileSets)
         {
             Destroy(tile);
         }
     }
 }

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.Vector2].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) FloorGenerator.PlaceFloor () (at Assets/Scripts/FloorGenerator.cs:53) FloorGenerator.DiscoverAreas () (at Assets/Scripts/FloorGenerator.cs:45) FloorGenerator.Start () (at Assets/Scripts/FloorGenerator.cs:17)

-Problem- The problem is, only the floor of the land is instantiated, the walls aren't, but the argument takes me to a part (line 53) which is the part where the floor is instantiated. The floor instantiating part is working, the wall part isn't, so I have no clue why it's taking me to the process of where the floor instantiates.

I've made the Vector 2 wall areas public in the unity editor, and when I start the game all of the vector 2 areas are found; just not used to instantiate. The wall instantiation for loop is exactly the same as the floor for loop, so I'm not sure what the problem is.

(CoinGenerator.instance.collumns/rows are just integers from another script that I'm using. They're not the problem, the problem is from instantiating with the list, not finding Vector 2's for the list)

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 unit_nick · Sep 30, 2017 at 04:30 AM

You are exceeding the size of the lists in your for loops. Change lines 52 & 62

 //for (int h = 0; h <= possibleFloorAreas.Count; h++)
 for (int h = 0; h < possibleFloorAreas.Count; h++)
 
 //for (int d = 0; d <= possibleWallAreas.Count; d++)
 for (int d = 0; d < possibleWallAreas.Count; d++)
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

69 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

Related Questions

Serialize List>??? 0 Answers

Really weird behaviour with boolean variables from custom class 2 Answers

Spawning more than one type of enemy script 3 Answers

How do you combine two lists of different classes into one? 1 Answer

C# find specific piece of data and where it is in list 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