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 upariver · Oct 31, 2016 at 10:49 AM · instantiatelistsout of bounds

Spawn List of game Objects

I am trying to spawn a list of GameObjects. I have been roaming the documentation and I have tried to do this:

Currently I am getting out of bounds argument. Could anyone point me in the right direction? Thanks in advance.

      public void spawntiles()
 {
     for (int i = 0; i < int_tile_spawnrate; i++)
     {
         int f = i - 1;
         Vector2 previous_tile_location;
         if (i == 0)
         {
             previous_tile_location = screenPosition;

             array_tiles.Add(Instantiate(Tiles, previous_tile_location, Quaternion.identity) as GameObject);

         }
         else
         {
             previous_tile_location = array_tiles[f].transform.position + array_tiles[f].GetComponent<Collider>().bounds.size;
             // var vector_previous_tile_position =array_tiles[f].transform.position;
             // vector_previous_tile_position.x = vector_previous_tile_position + Tiles.GetComponent<Collider>().bounds.size;
         }
        




     }
 }
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

3 Replies

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

Answer by Anton-Korhonen · Oct 31, 2016 at 10:10 PM

I would first store the reference to previous_tile_location outside the loop. Then inside your for-loop i would first spawn your Tiles object and then assign the previous_tile_location. This way every time the for-loop iterates, the object is instantiated to the location set in the last loop.

Something like this (untested):

     previous_tile_location = screenPosition;

     for (int i = 0; i < tile_spawnrate; i++)
     {
         array_tiles.Add(Instantiate(Tiles, previous_tile_location, Quaternion.identity) as GameObject);
         previous_tile_location = array_tiles[i].transform.position + array_tiles[i].GetComponent<Collider>().bounds.size;
     }

And if you want append the width of your collider to the position, you could use something like:

 previous_tile_location = array_tiles[i].transform.position + new Vector3(array_tiles[i].transform.position.x + array_tiles[i].GetComponent<Collider>().bounds.size.x, array_tiles[i].transform.position.y, array_tiles[i].transform.position.z));

Oh that horrible forum code formatting.

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 upariver · Nov 01, 2016 at 01:01 PM 0
Share

Thank you very much!

avatar image
1

Answer by EpicPants · Oct 31, 2016 at 10:58 AM

You don't add to your array in the else statement (where i!=0) so when i ==2 f will be 1 but your array count will be just 1. Basically you only add to your array when i == 0.

Comment
Add comment · Show 5 · 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 · Oct 31, 2016 at 11:02 AM 0
Share

That is one of the issues, thanks for helping out. However, I get nothing spawned on the screen at all either. Could you tell me why?

avatar image EpicPants upariver · Oct 31, 2016 at 11:20 AM 1
Share

Do they appear in the hierarchy? If so it may be an issue with positioning or scaling. I can't see any problems with your code at a glance but i'm not in a position to test it.

avatar image upariver EpicPants · Oct 31, 2016 at 01:51 PM 0
Share

Thank you for re$$anonymous$$ding me of the hierarchy. It was spawning, but it was spawning away from the screen.

Show more comments
avatar image upariver · Oct 31, 2016 at 02:57 PM 0
Share

Sorry to bother, but how would you get the width of the previous gameobject in the list?

avatar image
1

Answer by bishop87 · Oct 31, 2016 at 03:16 PM

try changing to i < int_tile_spawnrate to i < int_tile_spawnrate.length

Remove int f = i-1 and everyhwere that you have f on line 16/17 replace it with i.

also change if (i == 0) to if (i = 0)

Comment
Add comment · Show 3 · 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 · Oct 31, 2016 at 05:43 PM 0
Share

Thank you for answering. The thing is that I need to get the previous gameobject on the list's position and add it's length in order to spawn the next object in front of it.

avatar image JincSoft upariver · Nov 01, 2016 at 06:45 AM 1
Share

Well the part of the int f=i-1 will cause an out of bounds exception because the first time it loops it will be equal to -1 (arrays start at 0).

@bishop87 this is C# so if (i == 0) is correct, if(i=0) will throw an error because you are trying to assign 0 to i ins$$anonymous$$d of comparing the value.

avatar image bishop87 JincSoft · Nov 01, 2016 at 07:03 AM 0
Share

@JincSoft Ah yes you're right. I sometimes get confused about which is a value comparison and which one is a bool comparison in IF statements

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

72 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

Related Questions

How can I destroy my Instance without renaming it? 2 Answers

Dictionary Keys to Stack Enemy Types 1 Answer

Spawn gameobjects without overlap 0 Answers

Changing LocalScale in code affecting prefab stored in Assets 1 Answer

How to Instantiate Objects based on a list of integers 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