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

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

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

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

How to stop instantiating a certain object / objects from a list . 3 Answers

Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers

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