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 DonkeyMalonkey · Jan 30, 2018 at 01:38 PM · javascriptinstantiatearrayfor-loop

Instantiate object in for loop with array

So i am instantiating a GameObject which works just fine. Now i want to be able to save different instances of that GameObject and save those in an array and then display those via a for loop. For some reason My for loop isnt even executed.
here is the Code:

 for(var i : int = 0; i <= eventsArray.length-1; i++){
             print("i: "+i);
             DayDisplayInstance = Instantiate(DayDisplay, new Vector3(56.39999, -222.8), transform.rotation);
             DayDisplayInstance.transform.SetParent(startScreenPnl.transform, false);
             eventsArray.Add(DayDisplayInstance);
             DayDisplayInstance = eventsArray[i];
             child = DayDisplayInstance.transform.GetChild(0).gameObject;
             myText = child.GetComponent.<Text>();
             myText.text = "MO";                                             //Add variable instead of MO to make the event flexible
         }

If i Instantiate and add to the array outside of the loop it workds but that ofc will not allow me to instanciate multiple times in that one function, that's why i want to do it in the for loop.
My guess would be that the array is size 0 in the beginning and nothing is being added untill after the for loop. So the for loop says it starts at 0 and ends at array.length which since the array is empty is also 0. This results in the code in the for loop not being executed. How would i fix this? Any ideas? Anything would be much appreciated!
Thanks in advance!
.

(the Vector3 is only for testing for now. My thought would be to make an array with vectors and then looping through that with i as well to display those GameObjects in the right position.

Comment
Add comment · Show 1
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 Harinezumi · Jan 30, 2018 at 02:23 PM 0
Share

Hi,
I don't know UnityScript (that's why I write a comment and not an answer), but I think you expand your eventsArray within the loop when you use eventsArray.Add(DayDisplayInstance);. In C# the Add() function of List<T> expands the size of as necessary and assigns the value. So your loop will never finish, because eventsArray.length keeps increasing
Ins$$anonymous$$d you should first initialize your eventsArray to the desired size (which I think you already do), and then just use eventsArray[i] = DayDisplayInstance;.
Also, the line DayDisplayInstance = eventsArray[i]; is unnecessary, because you just assigned DayDisplayInstance to eventsArray[i].

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nighfox · Jan 30, 2018 at 02:26 PM

EDIT: Actually, it will not just not execute your code, but it will also crash Unity and cause massive memory consumption over time. @Harinezumi had already mentioned about it.

@Romano's answer works fine, but it needs a little bit of fix as that in itself may cause bugs later on. Here's what I've noticed so far:
1. Is eventsArray even an array? You're using its length property (which exists on arrays and not Lists) but then you're also using Add, which only is possible if it's a generic List.
2. You're adding items to eventsArray while accessing its actual length, which means this will continue to loop forever and ever.

Workaround: Just create a temporary variable to store the previous length of the list, then use that in your for loop.

 var length = eventsArray.Count;
 for(var i : int = 0; i < length; i++){
 //...your code here
 }

Somewhere in your code, (since you want to use Add), eventsArray should be declared as a List:

 import System.Collections.Generic;
 
 public var eventsArray : List.<GameObject>;

And initialize it like this:

 eventsArray = new List.<GameObject>();



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 DonkeyMalonkey · Jan 30, 2018 at 02:59 PM 0
Share

as i said no it doesnt execute the code below since the first eventsArray.Add(..) is within the loop which it kind of needs to be there to do what i want. So what kind of a work arround could i do? Im lost but i need this to work. It's the last thing that i need to get to work pretty much. Thanks in advance again!

avatar image Harinezumi DonkeyMalonkey · Jan 30, 2018 at 03:12 PM 1
Share

Use the variable / constant of the number of objects you want to instantiate in the loop condition (but use < ins$$anonymous$$d of @Nighfox this is why I deleted my comment, in the end Romano's answer will be correct :D :D :D

avatar image DonkeyMalonkey Harinezumi · Jan 30, 2018 at 03:55 PM 0
Share

well the number of GameObjects is exactly array.length-1 It's dynamic. Every time the fucntion gets called its one more.. Wait. I have an idea. I could make a counter that increases every time the function is called ( i have a function that is called by a button). That might work

Show more comments
Show more comments
avatar image
-1

Answer by Romano · Jan 30, 2018 at 02:05 PM

I think if you change this line

   for(var i : int = 0; i <= eventsArray.length-1; i++){

to read

  for(var i : int = 0; i <= 5; i++){

where 5 is the number of game objects you want to instantiate it should run. You would just replace the number 5 with whatever variable you want if the loop number needs to be dynamic.

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 Nighfox · Jan 30, 2018 at 02:21 PM 0
Share

In that case it won't be that dynamic as it would have been if it took the length property of array.

avatar image Romano Nighfox · Jan 30, 2018 at 02:24 PM 0
Share

If the events array is empty in the first place, I think that still won't run.

avatar image Harinezumi Nighfox · Jan 30, 2018 at 02:26 PM 1
Share

@Nightfox you wrote almost the same comment as the one I deleted :) But it doesn't solve the issue, because it is a logical bug ( eventsArray.Add(...) is called within the loop, increasing the length...).

avatar image Nighfox Harinezumi · Jan 30, 2018 at 02:29 PM 1
Share

Nice catch, I didn't notice that lol. $$anonymous$$ight post a workaround for that.

Show more comments

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

136 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

Related Questions

Avoiding instantiating two things in same location? 1 Answer

Array won't be filled by Instantiate 1 Answer

adding "X" amount of objects to an array 2 Answers

have array target person of most priority/stick with top prior 0 Answers

Trying to instantiate random enemy prefab from array 3 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