• 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 MartyrDan · May 17, 2014 at 04:34 AM · loopinfinite

What's wrong with this code and is there an easier way to write it!?

Hi, all I want to do is instantiate a prefab I already have setup at a random spawn point. The spawn points are just Empty Game Objects and there are a few of them setup on my map. I have written this code:

 #pragma strict
 
 private var spawnNum = 0;
 
 function Awake () {
     spawnTesting();
 }
 
 function spawnTesting () {
     WaitForSeconds(spawnNum * 50);
     spawnNum = Random.Range(0, 1);
     numCheck();
 }
 
 function numCheck() {
     if(spawnNum <= 0.1) {
         Debug.Log("Spawn1");
         spawnTesting();
     } else if (spawnNum <= 0.2) {
         Debug.Log("Spawn2");
         spawnTesting();
     } else if (spawnNum <= 0.3) {
         Debug.Log("Spawn3");
         spawnTesting();
     } else if (spawnNum <= 0.4) {
         Debug.Log("Spawn4");
         spawnTesting();
     } else if (spawnNum <= 0.5) {
         Debug.Log("Spawn5");
         spawnTesting();
     } else if (spawnNum <= 0.6) {
         Debug.Log("Spawn6");
         spawnTesting();
     } else if (spawnNum <= 0.7) {
         Debug.Log("Spawn7");
         spawnTesting();
     } else if (spawnNum <= 0.8) {
         Debug.Log("Spawn8");
         spawnTesting();
     } else if (spawnNum <= 0.9) {
         Debug.Log("Spawn9");
         spawnTesting();
     } else if (spawnNum <= 1) {
         Debug.Log("Spawn10");
         spawnTesting();
     } else {
         spawnTesting();
     }        
 }

So all I am doing is when the game is started, Awake calls a function to start a cycle to check a Random.Range and then calls a debug.log which will eventually be an instantiate moving it to the specific spawn point.

I think I have made some sort of infinite loop because my Unity keeps 'Not Responding' when I try to test the game. I am new to coding and would like to know if the way I am checking for the Random numbers is efficient enough or could be shortened and also how I can or could re write the code so I don't get an infinite loop going.

Thanks!

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 rgowen · May 17, 2014 at 04:51 AM 0
Share

When you're posting code, make sure you format it as such, otherwise it is just unreadable. Here's a prettier version:

 private var spawnNum = 0;
 
 function Awake() {
     spawnTesting();
 }
 
 function spawnTesting() {
     WaitForSeconds(spawnNum * 50);
     spawnNum = Random.Range(0, 1);
     numCheck();
 }
 
 function numCheck() {
     if (spawnNum <= 0.1) {
         Debug.Log("Spawn1");
         spawnTesting();
     } else if (spawnNum <= 0.2) {
         Debug.Log("Spawn2");
         spawnTesting();
     } else if (spawnNum <= 0.3) {
         Debug.Log("Spawn3");
         spawnTesting();
     } else if (spawnNum <= 0.4) {
         Debug.Log("Spawn4");
         spawnTesting();
     } else if (spawnNum <= 0.5) {
         Debug.Log("Spawn5");
         spawnTesting();
     } else if (spawnNum <= 0.6) {
         Debug.Log("Spawn6");
         spawnTesting();
     } else if (spawnNum <= 0.7) {
         Debug.Log("Spawn7");
         spawnTesting();
     } else if (spawnNum <= 0.8) {
         Debug.Log("Spawn8");
         spawnTesting();
     } else if (spawnNum <= 0.9) {
         Debug.Log("Spawn9");
         spawnTesting();
     } else if (spawnNum <= 1) {
         Debug.Log("Spawn10");
         spawnTesting();
     } else {
         spawnTesting();
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rgowen · May 17, 2014 at 05:03 AM

The short answer is yes, you are creating an infinite loop. You have two functions that call each other, and every possible code path in either function will execute the other one. Thus, when you call one, it will inevitably call the other one creating an infinite loop. What you're trying to do actually requires an infinite loop, but there's a better way to do it.
You should be using a while loop within your spawnTesting() coroutine to ensure that it keeps running, not by calling it from another function. Also you can avoid all those if statements by just concatenating the spawnNum into your Debug.Log() call.

 private var spawnNum = 0;
 
 function Awake() {
     spawnTesting();
 }
 
 function spawnTesting() {
     while(true) {
         WaitForSeconds(spawnNum * 50);
         spawnNum = Random.Range(0, 10);
         numCheck();
     }
 }
 
 function numCheck() {
     Debug.Log("Spawn" + spawnNum);
 }

If you have any questions feel free to ask.

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

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Instantiate prefab once help C# 1 Answer

I broke my game with what i think is an infinite loop? 2 Answers

When enemy health is 0, the player gets 73 XP, about 500,000 times. 2 Answers

Stop a animation loop after a certain amount of plays? 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