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 RumplyThrower · Jan 26, 2014 at 01:59 AM · crashgenerationgenerate

This code crashes unity, please help!

I use this code to generate a world in space of 500x500 and to then load it, it's not finished but for some reason it crashes when i start the GenerateWorld(); function.

 var generate : boolean = false;
 var load : boolean = false;
 var objects : GameObject[];
 var world = new Array(500);
 /*
 1 - sun
 2 - home planet (UNIQUE)
 3 - BLACK HOLE
 4 -> 11 - normal planet
 12 -> 15 - hot planet
 16 - cold planet
 */
 
 function SpaceCheck(x : int, y : int)
 {
     for(var i = x-3; i <= x+3; i++)
     {
         for(var j = y-3; j <= y+3; j++)
         {
             if(world[i][j]!=0)
             {
                 return 0;
             }
         }
     }
     return 1;
 }
 
 function GenerateWorld()
 {
     Debug.Log("Starting to generate");
     world[250][250] = 1;
     world[243][250] = 2;
     Debug.Log("Created home planet and solar system");
     for(var i = 20; i <= 480; i++)
     {
         for(var j = 20; j < 480; j++)
         {
             Debug.Log(i+" "+j);
             var a : float = Random.Range(0.0, 10.0);
             if(a >= 9.0)
             {
                 Debug.Log("Generated a Star");
                 world[i][j] = 1;
                 for(var k = i-10; k <= i+10; k++)
                 {
                     for(var l = j-10; l <= j+10; l++)
                     {
                         var b : float = Random.Range(0.0,10.0);
                         
                         if( (i-k)*(i-k)+(l-j)*(l-j)>=18 && b>=7.0)
                         {
                             if(SpaceCheck(k,l))
                             {
                                 var planet : int = 0;
                                 var dist : int = (i-k)*(i-k)+(l-j)*(l-j);
                                 if(dist >=18 && dist <=40)
                                 {
                                     planet = Random.Range(12,15);
                                 }
                                 if(dist>40 && dist <=150)
                                 {
                                     planet = Random.Range(4,11);
                                 }
                                 if(dist>150)
                                 {
                                     planet = 16;
                                 }
                                 world[k][l] = planet;
                             }
                         }
                     }
                 }
             }
         }
     }
     Debug.Log("done generatin");
 }
 
 function GetWorldData()
 {
     Debug.Log("done getting info");
 }
 
 function LoadWorld()
 {    
     for(var i = 1; i <= 500; i++)
     {
         for(var j = 1; j < 500; j++)
         {
             if(world[i][j])
             {
                 Instantiate (objects[world[i][j]], Vector3(i, j, 0), Quaternion.identity);
             }
         }
     }
     Debug.Log("done loading");
 }
 
 function Update () 
 {
     if(generate)
     {
         generate = false;
         GenerateWorld();
     }
     if(load)
     {
         load = false;
         GetWorldData();
         LoadWorld();
     }
 }
 
 function Start()
 {
     for (var i = 0; i < 500; i++)
     {
         world[i] = new Array(500);
     }
 }
Comment
Add comment · Show 4
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 Benproductions1 · Jan 26, 2014 at 02:45 AM 1
Share

It's not clear at all what that code is supposed to do. Where do all these numbers come from? What do they mean? Why 3? Why 0? Why 1? Why 250? Why 20? Why 400? Why 10.0? Why 9? Why 18? Why 40? Why 15? Why 12? Why 16? Why 4? Why 11? Why -10? Why 150?

Even you can't tell me you can remember what all of those are supposed to mean???

avatar image RumplyThrower · Jan 26, 2014 at 10:38 AM 0
Share

The values of world at i,j coordinates represent an object(the way it's decided is in the comment of the code at the top). In the function SpaceCheck we check the neighbouring fields around a certain coordinate to see if there is another object there, for example in this case: 000 0X0 000 the function would return 1 meaning it is ok for the planet/start/whatever to be placed there The random variables are there just to randomly generate if there is going to be a planet or not at a certain coordinate. I could go on about this for a while but i don't think it's important for the reason it's crashing.

avatar image RumplyThrower · Jan 26, 2014 at 10:49 AM 0
Share

ok so here is what the code(at least the GenerateWorld() part) does in short:

  1. check if the order to generate has been given

  2. start at coordinates 20, 20 and randomly generate a number for each coordinate

  3. if the number is >=9.0 generate a start (place 1 at world(i,j))

  4. create a solar system for the star, starting from starX-10, starY-10 coordinates to starX+10, starY+10 coordinates and generate a random number

  5. if the number is >= 7.0 check if there are already any planets nearby in the "world" matrix(2d array) by using the function SpaceCheck(x : int, y : int)

  6. if the fucntion returns 1, the area is clear and planet can be generated

  7. based on the distance from the star(center of the solar system) generate randomly one of 3 types of planets(the values for them are written in the comments)

avatar image Benproductions1 · Jan 26, 2014 at 10:53 AM 0
Share

I think it would help increadably if you at least commented your code, or even just used constants that describe what each of the values mean. It's very hard to debug without knowing what it's doing, impossible when there's not even a way to find out.

Also, you should be using a different collection for your world. As it seems you're just creating a 500x500 grid, you should probably be using build-in arrays (`int[,]`)

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Script Crashing Unity 1 Answer

Making opening door requires a Key 2 Answers

What is wrong with this script? 2 Answers

need help, with greater than statment 1 Answer

How to get a value from an array within another 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