Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 godwintineractive · Feb 23, 2015 at 12:54 PM · errorbugsroguelike

erros and bugs

I'm trying to make 2d roguelike game but somehow the image looks scary. here are the error's that I received from the console.

/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) BoardManager.RandomPosition () (at Assets/Script/BoardManager.cs:69) BoardManager.LayoutObjectAtRandom (UnityEngine.GameObject[] tileArray, Int32 minimum, Int32 maximum) (at Assets/Script/BoardManager.cs:79) BoardManager.SetupScene (Int32 level) (at Assets/Script/BoardManager.cs:90) GameManager.InitGame () (at Assets/Script/GameManager.cs:30) GameManager.Awake () (at Assets/Script/GameManager.cs:25)

all of my sprites are 254x254.

 using UnityEngine;
 using System;
 using System.Collections.Generic;
 using Random = UnityEngine.Random;
 
 public class BoardManager : MonoBehaviour 
     {
         [Serializable]
         public class Count
         {
             public int minimum;
             public int maximum;
         
             public Count (int min, int max)
             {
                 minimum = min;
                 maximum = max;
             }
     }
 
         public int columns = 8;
         public int rows = 8;
         public Count wallCount = new Count (5,9);
         public Count foodCount = new Count (1,5);
         public GameObject exit;
         public GameObject[] floorTiles;
         public GameObject[] wallTiles;
         public GameObject[] foodTiles;
         public GameObject[] enemyTiles;
         public GameObject[] outerWallTiles;
 
         private Transform boardHolder;
         private List <Vector3> gridPositions = new List<Vector3>();
 
         void InitialiseList()
         {
             gridPositions.Clear ();
             for (int x =1; x < columns - 1; x++)
             {
                 for (int y = 1; y < rows - 1;y++)
                 {
                     gridPositions.Add(new Vector3(x,y,0f));
                 }
             }
         }
 
     void BoardSetup()
     {
             boardHolder = new GameObject ("Board").transform;
             for (int x = -1; x < columns + 1; x++)
             {
                 for (int y = -1; y < rows + 1; y++)
                 {
                     GameObject toInstantiate = floorTiles[Random.Range (0, floorTiles.Length)];
                     if (x == -1 || x == columns || y == -1 || y == rows)
                         toInstantiate = outerWallTiles[Random.Range (0, outerWallTiles.Length)];
                     
                     GameObject instance = Instantiate(toInstantiate, new Vector3 (x,y,0f), Quaternion.identity) as GameObject;
 
                     instance.transform.SetParent(boardHolder);
 
                 }
             }
     }
     
     Vector3 RandomPosition()
         {
             int randomIndex = Random.Range(0,gridPositions.Count);
             Vector3 randomPosition = gridPositions [randomIndex];
             gridPositions.RemoveAt (randomIndex);
             return randomPosition;
         }
 
         void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum)
         {
             int objectCount = Random.Range (minimum, maximum + 1);
             for (int i = 0; i < objectCount; i++)
             {
                 Vector3 randomPosition = RandomPosition();
                 GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];
                 Instantiate (tileChoice, randomPosition, Quaternion.identity);
             }
         
         }
 
         public void SetupScene (int level)
         {
             BoardSetup ();
             InitialiseList ();
             LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
             LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum);    
             int enemyCount = (int)Mathf.Log (level, 2f);
             LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);
             Instantiate (exit, new Vector3 (columns - 1, rows - 1, 0f), Quaternion.identity);
 
         }
     }
 
 

also I think my code is perfectly fine.

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 gjf · Feb 23, 2015 at 10:32 AM 1
Share

what is the actual error message?

avatar image Graham-Dunnett ♦♦ · Feb 23, 2015 at 12:54 PM 2
Share

If your code was perfectly fine it wouldn't cause a crash.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Feb 23, 2015 at 03:18 PM

Doing a Google search for your error's line number and class

System.Collections.Generic/List.cs:633

shows it's an array out of bounds/range exception as you can probably see on your machine from the complete error message that you should've pasted here :).

The stacktrace also shows it's coming from inside your method
Vector3 RandomPosition()
from the line
Vector3 randomPosition = gridPositions [randomIndex];.

The only way this line can throw an out of bounds exception (since you are doing int randomIndex = Random.Range(0,gridPositions.Count);) is if the list doesn't have any items in it ( gridPositions.Count == 0 )

Since this is all the code we can see and we can't see how you are calling this method, we can't really tell what is the root of your problem.

One thing sticks out for me though, You are populating gridPositions with for loops

 for (int x =1; x < columns - 1; x++)
 {
      for (int y = 1; y < rows - 1;y++)
      {
          gridPositions.Add(new Vector3(x,y,0f));

This means that with columns/rows set to 2 for example, you would be adding zero items to the list.

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 Bonfire-Boy · Feb 23, 2015 at 03:34 PM 0
Share

Also, since gridPositions is depleted each time a position is used, it could be emptied by the process. So you definitely need to check that. Could just change line 77 to

 for (int i = 0; i < objectCount && gridPositions.Count > 0; i++)
 

(although you probably want a warning in that situation too)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity C# code not register, other PC can 0 Answers

Why does empty if statement fix null reference 1 Answer

Error "OnTriggerEnter: this message has to be of type: Collider" 0 Answers

Exporting to iOS device breaks game and causes errors 0 Answers

Strange bug after update to 5.6.0f1 (This is not supported) 2 Answers


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