• 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 /
This question was closed May 29, 2015 at 10:22 AM by galaboy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by galaboy · May 29, 2015 at 06:04 AM · scripting beginnerarraysscriptingbasics

creating prefabs in array

hi, i get an error message stating that the array index is out of range. i need help on where am i making mistake. please explain me wat is the error. i have only one gameobject and i want only that object to be instantiated in loop.

 using UnityEngine;
 using System.Collections;
 
 public class spawner_1 : MonoBehaviour {
     
     //public GameObject card;
     //public GameObject card2;
     
     public GameObject[] prefabPool = new GameObject[1];
     
     // Use this for initialization
     void Start () {        
 
     prefabCreation ();
         //prefabPool = new GameObject[2];
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     public void prefabCreation()
     {
         //print ("method start");
 
         for(int i = 0; i < 4; i++)
         {
             //print(i);
 
             for(int j = 0; j < 4; j++)
             {
                 Vector3 position = new Vector3((j * transform.localScale.x) -1.0f, (j * transform.localScale.y) + 1, 0);
                 Instantiate(prefabPool[i],position, Quaternion.identity );
             }
         }
     }
 }
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by taxvi · May 29, 2015 at 07:09 AM

on line 9 you create an array of two elements while in the the for loop on line 27 you try to access the first 4 elements of that array

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 galaboy · May 29, 2015 at 07:41 AM 0
Share

thanks for the response. i tried myself and figured out what is the mistake.

avatar image
1

Answer by karma0413 · May 29, 2015 at 09:17 AM

Array index out of range means that you are trying to access a position in the array, that does not exist.

Put in some debug lines that check: 1. How big your array truthfully is 2. Where you are at in terms of accessing it.

 // First, check how big the array truthfully is...
 int a = prefabpool.Length;

You should note that even you have 7 items in your array as an example.... Then the array starts at 0 and ends at 6. Arrays always start at zero

 print (a);
 print (i); // Check which spot in the array you really are trying to use
 
 






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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Can you trick a Ui Button that it is being clicked with code? 3 Answers

How to get multiple string values from override ToString()? 1 Answer

How to close up charactor ? And move charactor to charactor 2D ? 0 Answers

Camera that follows rotating object with mouse input 0 Answers

how to allow the key to only open 1 door rather than all of them? 0 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