• 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 upariver · Apr 26, 2016 at 08:13 AM · c#arrayspriteboundsout of bounds

[C#] Array out of bounds

I'm trying to make a line of prefabs that spawn next to each other depending on their sprite size. I am however getting a out of bounds error, but I can't seem to find out why.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Spawner : MonoBehaviour {
 
     public int numPlat;
     public GameObject[] PlatSpawner;
     public GameObject PlatformToSpawn;
     public Vector2 spawnPosition;
     public Vector2 NextSpawnPosition;
     public SpriteRenderer spriteChosen;
     // Use this for initialization
     void Start () {
         
         Debug.Log("Number" + numPlat);
         spawnStuff();
         
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     private void Awake()
     {
 
     }
 
     public void spawnStuff()
     {
         
        
         for (int i = 0; i < numPlat; i++) 
         {
             if (i == 0)
             {
                 PlatSpawner[0] = Instantiate(PlatformToSpawn, transform.position, Quaternion.identity) as GameObject;
             }
             if (i >= 1)
             {
                 spawnPosition = PlatSpawner[i].transform.position.x + new Vector3( spriteChosen.bounds.size.x,0);
 
                 PlatSpawner[i] = Instantiate(PlatformToSpawn, spawnPosition, Quaternion.identity) as GameObject;
                 Debug.Log("ds");
             }
         }
     }
 
 }

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 RakshithAnand · Apr 26, 2016 at 11:10 AM 0
Share

What is the use of numPlat? You can directly use PlatSpawner.Length to find the lenght of the array. Because numPlat maybe different from array length you are getting out of bounds

avatar image upariver RakshithAnand · Apr 26, 2016 at 11:52 AM 0
Share

It allows you to say how many blocks you want spawned, so it spawns a whole line of platforms, depending on the size you want it to be.

1 Reply

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

Answer by Ali-hatem · Apr 26, 2016 at 12:03 PM

what about this simple :

 int Nextpos;
 Vector3 pos;
 public GameObject [] obj;
 void Start(){
     for (int i = 0; i < obj.Length; i++) {
         Nextpos ++;
         pos = new Vector3 (transform.position.x + Nextpos, transform.position.y, transform.position.z);
         Instantiate (obj [i], pos, transform.rotation);
     }
 }

& just manipulate the object scale size to reduce the gap between objects . & as RakshithAnand said no need to numPlat this way is more dynamic doesn't mater how many objects you add it will work.

Comment
Add comment · Show 4 · 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 upariver · Apr 26, 2016 at 01:59 PM 0
Share

But what If I want to spawn several instances of only one object type?

avatar image upariver · Apr 26, 2016 at 02:10 PM 0
Share

Also, int Nextpos? Shouldn't it be Vector3D Nextpos ins$$anonymous$$d?

avatar image upariver · Apr 26, 2016 at 02:25 PM 0
Share

I figured it out. For those interested, just replace the obj[i] with a var that you declared as

 public GameObject PlatTospawn;

then replace it for obj[i], that way you can spawn only one instance of that. $$anonymous$$any thanks Ali and RakshithAnand!

avatar image Ali-hatem upariver · Apr 26, 2016 at 06:49 PM 0
Share

tailor it so it fits you . & i tried Vector3 Nextpos bt it was messy until i figured out it needs int & we can assign it to xyz as desired. also you can use i ins$$anonymous$$d of Nextpos++

 transform.position.x + i

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

159 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

Related Questions

problem with arrays and image replacment 0 Answers

if ALL items in array are something 1 Answer

(Vector Graphics package) Sprite Editor - Custom Physics shape not working properly for the vector sprites 0 Answers

2D Arrays Help. Why to use 2D arrays for 2 ints 2 Answers

Convert string to array name? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges