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 sandpixels · Dec 18, 2016 at 07:55 AM · javascriptinstantiatewindowsinvokerepeatingerror-handling

My script keeps crashing my Unity Editor Client. Please help

So I created this script recently to spawn a "enemyPrefab" every x seconds. But when the function is called on my Unity client just crashes and I have to open up Task Manager and manually End Task it for it to close.

Here's my script:

 #pragma strict
 
 //Variable list
 public var spawner : Transform[];
 public var spawnTime : float;
 public var enemyPrefab : GameObject;
 private var spawner_num : int;
 
 function Start () 
 {
 //Call the SpawnEnemy function every spawnTime seconds
 InvokeRepeating ("SpawnEnemy", spawnTime, spawnTime);
 }
 
 function SpawnEnemy ()
 {
 //Tells the log that the function has been called upon
 Debug.Log("Started");
 while (true)
     {
     //Picks a random spawner to spawn "enemyPrefab" out of
     spawner_num = Random.Range(0,3);

     //Tells log what spawner has been picked
     Debug.Log("Spawner is spawner number " + spawner_num);

     //Spawns the "enemyPrefab" at the spawner location
     Instantiate(enemyPrefab, spawner[spawner_num].position, spawner[spawner_num].rotation );
     }
 //Tells the log the function has ended.
 Debug.Log("Ended");
 }

Thank you so much if you can help me out on this, I've been trying to advance my game for about 30 minutes now googling this topic but can't find the answer, but anything helps! :) (P.S. I'm on Windows if that helps any.)

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

2 Replies

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

Answer by ElijahShadbolt · Dec 18, 2016 at 08:27 AM

Get rid of the while loop! while(true) goes on forever without stopping, since there are no yield or break statements inside the loop.

The InvokeRepeating() function already handles the looping (since it calls SpawnEnemy() every spawnTime seconds), so there is no need for a loop inside your function.

Comment
Add comment · Show 2 · 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 sandpixels · Dec 18, 2016 at 08:44 AM 0
Share

Oh! You're right, that's probably spiraling my script out of control, I'll try it right now! Thanks for the help. I'm still quite new to coding so I'll make sure to remember this next time lol!

avatar image sandpixels · Dec 18, 2016 at 08:47 AM 0
Share

So I tried this and the script seems more clean but my client still stops responding, could it just be something wrong with my client or computer then?

avatar image
1

Answer by sandpixels · Dec 18, 2016 at 10:52 AM

I found my issue, my Firewall was interfering with my Editor and crashing it for some reason, moved around some permission and now I could do it safely! @SublimeGamer Thanks for telling me about the while loop ahead of time! It helped alot ;D

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 ElijahShadbolt · Dec 18, 2016 at 09:53 PM 0
Share

No problem :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

129 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

Related Questions

Limit an amount of prefabs instantiated in javascript for Unity 5. 0 Answers

Having trouble Instantiating. 0 Answers

Issue with Raycast hit.distance Variables Reseting Itself 0 Answers

Why wont my script instantiate multiple instances? 1 Answer

Unity3d 4.6 javascript prefab with script 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