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 Pueken · Mar 16, 2017 at 08:36 PM · freezenoobstuckrandomspawning

Unity random generator kills programm

Hey everyone, first of all i am a complete Unity noob so please excuse any dumb questions: I am trying to make a 3D Jump 'n Run in Unity in which you have to complete a random Stage playing as a Ball. The Stage consists of 3 lanes, each 10 Units apart, to which the Ball can Jump to. Right now i am trying to make the randomizer which creates the Stage (The "Stage" consists of random Platforms scattered across the 3 lanes).

 #pragma strict
 
 var zPos = 7.5;
 var xPos = 0;
 var xHelp;
 var ReMax = 15;
 var Re = 0;
 var Ground : GameObject;
 
 
 
 function Start () {
     Ground = GameObject.Find("Ground");
 
     while (Re< ReMax){
         if (xPos == 0){
             xHelp= Random.Range(1,4);
             Debug.Log (Random.Range);
             if (xHelp== 1){
                 xPos = -10;
                 transform.Translate(xPos,0,0);
                 Instantiate(Ground);
                 transform.Translate(-xPos,0,0);
                 Re += Re;
                 transform.Translate(0,0,zPos);
             }
             if (xHelp== 2){
                 xPos = 10;
                 transform.Translate(xPos,0,0);
                 Instantiate(Ground);
                 transform.Translate(-xPos,0,0);
                 Re+= Re;
                 transform.Translate(0,0,zPos);
             }
             if (xHelp== 3){
                 xPos = 0;
                 transform.Translate(xPos,0,0);
                 Instantiate(Ground);
                 transform.Translate(-xPos,0,0);
                 Re += Re;
                 transform.Translate(0,0,zPos);
             }
             
         }
 
     }
 
 }

Unity itself doesnt give me an Errormessage when i try to run the Script but the program just frezzes as soon as i hit play. The Gameobject "Ground" are the Platforms the program is supposed to create. I have never worked with Instantiate so i kinda suspect my error to be the spawning of the Platforms. I've been stuck here for a while and dont realy know how to go on, so if someone would have a suggestion, i'd be very grateful.

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
1

Answer by Commoble · Mar 16, 2017 at 09:12 PM

You have an infinite loop in your Start function, which is why your game is freezing. Your loop is infinite because Random.Range(1,3) will only ever return the integers 1 or 2. When you use Random.Range() with integers, the range of possibilities start from the first number and exclude the last number. The way your loop is set up, it will never end.

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 Pueken · Mar 16, 2017 at 09:26 PM 0
Share

Right, thanks for your reply first of all, so i just adjusted the numbers to Random.Range(1,4) and also changed the numbers of xHelp to 1,2 and 3 because i dont know what i was thinking setting them to 0,1 and 3, but that still did not change my problem of the Game freezing.

avatar image tanoshimi Pueken · Mar 16, 2017 at 09:50 PM 1
Share

You've still got an infinite loop : as soon as xPos != 0, nothing is ever going to increment Re. And what on earth are all those transform.Translates meant to achieve? They'll just move the object to which this script is attached - is that what you want?

avatar image RobAnthem Pueken · Mar 16, 2017 at 10:07 PM 0
Share

Since you are a new developer, I suggest that you start ASAP using some coding conventions. The C based conventions are pretty solid, but since you are using JS, I'm sure JS has some decent standard conventions. Just make your code easier to read for you, and everyone else.

Also, While loops are infinite loops until you break the while parameter. So your issue being Re always equals less than Re$$anonymous$$ax.

avatar image
1

Answer by jjcrawley · Mar 17, 2017 at 12:30 AM

Can confirm, infinite loop. This issue can happen when you least expect, may be good practice to stick in a max number of iteration cycles. May help to prevent the editor from locking up. Or Unity could provide a quick stop feature in case this situation happens, that doesn't involve restarting the editor.

The loop is going to keep executing until the condition evaluates to false. Your condition is:

 Re < ReMax

Re is 0, therefore doing Re += Re gives you zero, as 0 + 0 = 0. Therefore, Re is never increasing, as a consequence it will never be larger than ReMax, hence the infinite loop. If you are setting Re in the editor then make sure that it isn't 0, or less than 0.

One other thing worth mentioning, if xPos is not 0, then Re is never increased. If Re is never increased then it will never exit the loop. Why not change over to a for loop? May be safer, and you can specify a maximum number of tiles to place. Same principle, you'll just be iterating until you've placed the maximum number of tiles, rather than managing the counter manually in a while loop.

 for(int iteration = 0; iteration < maxNumberTiles; iteration++)
 {
        //Place your tiles
 }

That should remove the editor freezing issue.

Hopefully this helps.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

noobHell, prefab arrays, GameObjects, Random Spawning 1 Answer

Game random freeze / hang up / stuck 0 Answers

Stuck on splash screen on Android only 0 Answers

Player gets stuck after AddForce in Air 3 Answers

Game freezes, cannot see why 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