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 /
  • 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.

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

if ALL items in array are something 1 Answer

problem with arrays and image replacment 0 Answers

Error CS1061: Are you missing an assembly reference? 2 Answers

How to make a 2D array of buttons? 2 Answers

How to instantiate bullets in multiple transforms 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