Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MKayJay · Jun 23, 2014 at 09:23 PM · instantiatefor-loopspawnpointsfor loop

Instantiate script crashes unity

So I'm making a simple spawn system for my horror game, which is going to spawn things like notes and batteries at random on the game start at certain predefined spawnpoints, but I have a problem with the script crashing. The script works fine if I only instantiate one object- Here's the script:

 public class SpawnSystem : MonoBehaviour 
     {
         public GameObject ObjectToSpawn;
         private List<GameObject> _spawnedObjects;
     
         // Use this for initialization
         void Start () 
         {
             _spawnedObjects = new List<GameObject>();
             for(int i = 0; i < transform.childCount; i++)
             {
                 _spawnedObjects.Add((GameObject)Instantiate(ObjectToSpawn));
                 _spawnedObjects[i].transform.parent = transform;
                 _spawnedObjects[i].transform.position = transform.GetChild(i).position;
                 _spawnedObjects[i].transform.rotation = transform.GetChild(i).rotation;
             }
     
         }
     }

At first I thought it crashed because I simply had a local variable inside the for loop holding the instantiated value, but making a list of gameobjects to contain them didn't help. The spawn points are simply empty gameobjects which are children to the object this script is attached to. The observed behavior is that I start my game, sounds starts playing, as it usually does while the game is loading, but after a few seconds the sound stops, and everything freezes. Am I not allowed to instantiate this many objects in one cycle? (currently I'm only testing with 4 objects)

Any help would be appreciated.

Thanks for reading.

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 Jeff-Kesselman · Jun 23, 2014 at 09:24 PM 0
Share

What do you mean by "crashing"? be more specific.

Does the game just stop playing and return to the editor?

Does unity actually crash and die such that you have to restart unity?

avatar image MKayJay · Jun 23, 2014 at 10:00 PM 0
Share

When I say crash, I mean everything freezes as to the point where I have to forcibly close unity through the task manager in order to get moving- It's totally frozen, as if in an infinite loop, or is loading something so massively that it just freezes.

I am currently testing with "ObjectToSpawn" being a battery object- It's just a prefab with a model, a collider, and a script. I've just tried removing the script, with no change in behavior.

I currently only have 1 object with this script attached.

I am not sure what else information there is to give you that seems relevant?

There is no "hocus pocus" about the object being spawned, or the object spawning the objects. Basically it's like this:

There's a parent object with the above script attached. It has 4 child objects, that are pure empty gameobjects. Currently there's not even anything random about this, it just iterates through the children in the code, and is supposed to spawn a battery at each location. The children holds all the information I need from that in the transform (Position, and rotation).

I am not sure what else relevant information I have to give? As I stated earlier this works when I just instantiate 1 object (No for loop).

Thanks for your time :-)

3 Replies

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

Answer by Bunnybomb7670 · Jun 23, 2014 at 10:28 PM

I think it goes into an infinite loop by doing the for loop based on the childCount. You basically loop for every child, then in your loop add another child and have to loop again, and so on. You need to store the size before the loop I think to prevent it from infinitely looping through children. Setting the Transform.parent is the issue because it makes it into a child, therefore increasing the child count.

Simply store an integer of the childcount before the loop, then use that as the length of the loop, rather than accessing the childcount value.

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 MKayJay · Jun 30, 2014 at 12:01 PM 0
Share

Changed the "accepted answer" to yours-. Since my answers are being moderated , they take a while to appear, so I think I posted my own answer earlier than posted above- But nevertheless you spend time on helping me, so you deserve the "accepted answer" :-) Thanks a lot.

avatar image
1

Answer by Bunnybomb7670 · Jun 23, 2014 at 09:31 PM

I cannot tell much from what you have posted, but by any chance is the variable : ObjectToSpawn have a script : SpawnSystem on it? If so, that script will basically spawn however many objects it should, then if they have spawnsystems on them, they will all spawn however many spawnsystems they have been told to and it will continue like that in an infinite loop. That is the only reason I can see why it is crashing unless you give us more information.

Comment
Add comment · 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
0

Answer by MKayJay · Jun 23, 2014 at 10:29 PM

So I just figured it out myself.

The problem is I am taking the childcount information directly from the transform, which means, that everytime I spawn an object, the child count goes up- thus we end up with an infinite loop. So the solution is easy, just cache the initial childcount in a variable before going into the for loop... phew., right infront of my eyes :P

Thank you guys for your time- I hope this will be helpful for someone doing the same mistake as me down the road lol. :-)

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 Bunnybomb7670 · Jun 23, 2014 at 10:30 PM 0
Share

I literally wrote that solution 1 $$anonymous$$ute before you posted :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

What's best: Instantiate or LoadLevelAdditive? 2 Answers

2.5d game instantiate problem 1 Answer

loading elements on startup 2 Answers

GUI Button and for loop not working 2 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