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 Razputin · Apr 03, 2014 at 01:21 AM · buildrandomlevelsettingsgenerated

How to save a scene that you randomly generated as a new level in your build settings?

I am randomly generating the overworld of my game. When the player enters a city they are loaded into a level of that city. Then when they exit the level I would like them to return to the same previously randomly generated overworld, however currently when they return it generates a new overworld.

So I suppose my question is, how would I save the generated world to return to later?

Here is how i'm generating the world.

 public class CubeGrid : MonoBehaviour {
  
     //these are now GameObjects so you can use Prefabs to define what they are
     public GameObject border;
     public GameObject grassLand;
     public GameObject house;
     public GameObject city;
     public GameObject tree;
     public GameObject wolf;
     public GameObject bandit;
  
     public int randNum = 0;
     public int xCubes;
     public int zCubes;
  
     public bool fighting = false;
     public bool enterWolf = false;
  
     void Start () {
        GenerateLandscapeAndObjects(xCubes,zCubes);
        GenerateBorder(xCubes,zCubes);
     }
  
     private void GenerateLandscapeAndObjects(int xMax, int zMax) {
  
        GameObject landscape = new GameObject("Landscape and Objects");
  
        for ( int z = 0; z < zMax; z++ )
        {
          for ( int x = 0; x < xMax; x++ )
          {
           float rnd = Random.value;
           if ( rnd < 0.01 )
           {
               DoInstantiate(city, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
           }
           else if (rnd < 0.07)
           {
               DoInstantiate(tree, new Vector3(x,0,z), Quaternion.identity,landscape.transform);
           }
           else if ( rnd < 0.15 )
           {
               DoInstantiate(house, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
           }
           else 
           {
               DoInstantiate(grassLand, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
  
               randNum = Random.Range(0,50);
               if(randNum <= 3){
                  DoInstantiate(wolf,new Vector3(x,0,z), Quaternion.identity,landscape.transform);
               }
               else if(randNum ==4){
                  DoInstantiate(bandit, new Vector3(x,0,z), Quaternion.identity,landscape.transform);
               }
           }
          }
        }
     }
  
     private void GenerateBorder(int xMax, int zMax) {
  
        GameObject borderGameObject = new GameObject("Landscape and Objects");
  
        for ( int x = 0; x < xMax; x++ )
        {
          DoInstantiate(border, new Vector3(x,1,0), Quaternion.identity,borderGameObject.transform);
          DoInstantiate(border, new Vector3(x,1,zMax), Quaternion.identity,borderGameObject.transform);
        }
  
        //create border blocks along z axes
        for ( int z = 0; z < zMax; z++ )
        {
          DoInstantiate(border, new Vector3(0,1,z), Quaternion.identity,borderGameObject.transform);
          DoInstantiate(border, new Vector3(xMax,1,z), Quaternion.identity,borderGameObject.transform);
        }
     }
  
     private void DoInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) {
        Transform temp = ((GameObject)Instantiate(prefab,position,rotation)).transform;
        temp.parent = parent;
     }


Heres how I get inside buildings

        if(guiOn && enterCity == true)
        { 
          if(GUI.Button(new Rect(10,200,1200,100), "Visit the inn"))
          {
           Application.LoadLevel("InsideTheInn");
          }

And heres how I return to the overworld.

 public class InnCameraScript : MonoBehaviour {
  
     void OnGUI(){
        if(GUI.Button(new Rect(10,400,1200,100), "Leave the tavern.")){
        }
        Application.LoadLevel("Map Creator");
     }
 }

However obviously that's where it creates a new map because its loading the first script again.

So my question is how would I make that first script save the world it generated as a new level in the build settings so that I could refer to it later in scripts similar to the third script I posted.

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 Razputin · Apr 03, 2014 at 02:30 AM 0
Share

That seems like what I would need to use, but i'm not sure how it gets implemented into the code. Does it go in at the end of each time I generate something? private void GenerateBorder(int x$$anonymous$$ax, int z$$anonymous$$ax) {

     `
     
     GameObject borderGameObject = new GameObject("Landscape and Objects");
     
     
     for ( int x = 0; x < x$$anonymous$$ax; x++ )
     {
         DoInstantiate(border, new Vector3(x,1,0), Quaternion.identity,borderGameObject.transform);
         DoInstantiate(border, new Vector3(x,1,z$$anonymous$$ax), Quaternion.identity,borderGameObject.transform);
     }
     
     
     for ( int z = 0; z < z$$anonymous$$ax; z++ )
     {
         DoInstantiate(border, new Vector3(0,1,z), Quaternion.identity,borderGameObject.transform);
         DoInstantiate(border, new Vector3(x$$anonymous$$ax,1,z), Quaternion.identity,borderGameObject.transform);
     }
     DontDestroyOnLoad(transform.gameObject);

 }`
avatar image getyour411 · Apr 03, 2014 at 02:36 AM 0
Share

Once, in Awake()

avatar image Razputin · Apr 03, 2014 at 02:40 AM 0
Share

Ahh okay its working, now I just have to add a variable so it doesnt create a new world, and save the old one each time. Right now its for$$anonymous$$g a second random world on top of the old one. But I should be able to fix that easy. Thanks for the help, that was pretty simple.

avatar image getyour411 · Apr 03, 2014 at 02:49 AM 0
Share

good deal, I changed that comment to an Answer.

1 Reply

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

Answer by getyour411 · Apr 03, 2014 at 01:45 AM

Take a look at DontDestroyOnLoad

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

20 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

Related Questions

How to save a scene that you randomly generated as a new level in your build settings? 1 Answer

How to save a level that was randomly generated, so it can be returned to later. 1 Answer

How can i make an infinite level? 4 Answers

Post-Build Custom Level Loading 2 Answers

Randomly Generated 2D Level 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