Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 17, 2014 at 12:28 AM · destroyrandomloadgridapplication

Don't destroy on load not working?

I randomly create a grid based world using this script. However if my player enters a building, then returns to the over world it is still recreated.

 using UnityEngine;
 using System.Collections;
 
 public class CubeGrid : MonoBehaviour {
     
     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);
     }
 
     void Awake() {
         DontDestroyOnLoad(transform.gameObject);
     }
     
     private void GenerateLandscapeAndObjects(int xMax, int zMax) {
         
         //create a GameObject to contain the landscape and objects
         //to keep things neat in the Hierarchy tab if you decide
         //to start generating huge landscapes later on
         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) {
         
         //create a GameObject to contain the border
         //to keep things neat in the Hierarchy tab if you decide
         //to start generating huge landscapes later on
         GameObject borderGameObject = new GameObject("Landscape and Objects");
         //create border blocks along x axes
         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);
         }
 
     }
     
     //A helper function to let you organise GameObjects easily in 
     //the hierarchy by setting the transform.parent of the instantiated prefab.
     private void DoInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) {
         Transform temp = ((GameObject)Instantiate(prefab,position,rotation)).transform;
         temp.parent = parent;
     }
 }
 

Example script for being inside a house

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

Can someone tell me why don't destroy isn't working?

Comment
Add comment · Show 2
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 · Apr 17, 2014 at 03:45 AM 0
Share

DontDestroyOnLoad doesn't prevent the object from being reloaded from the scene. It just prevents it from being destroyed when loading a new scene.

avatar image Willard720 · Jul 25, 2018 at 01:36 AM 0
Share

Jesus, please don't include the code that isn't releavent. Use common sense, public int cubeX isn't the issue.

4 Replies

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

Answer by fendorio · Apr 17, 2014 at 03:58 AM

What's happening is another is being created. You could add this to your script to make it persistent and destroy further objects of the same type that get created on loading the scene again.

  //Needs to be static.
  private static bool spawned = false;

  void Awake()
  {
       if(spawned == false)
          {
               spawned = true;

               DontDestroyOnLoad(gameObject);

               //Code...
          }
       else
          {
                DestroyImmediate(gameObject); //This deletes the new object/s that you
                                              // mentioned were being created
          }
   }

So yeah what's happening here is that the first time you create this, spawned is false. So the first object makes a call to the DontDestroyOnLoad(), which as the name suggests, means the first object will persist through scenes.

This is why we set the static bool spawned to true, static meaning it's 'shared' between all instances of this object.

Now when you load that scene again, 'it's' going to create another GameObject of this type, along with everything else in the hierarchy.

So the second time you load this scene, a second instance of the GameObject is created, however because the first GameObject set the static bool spawned to true, it's true when the second GameObject is created, thus the else block is entered and the second object is destroyed :)

Comment
Add comment · Show 13 · 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 Razputin · Apr 17, 2014 at 02:40 PM 0
Share

Alright I understand what you've written does, but where you've put //code am I actually moving anything there? If I just use this code verbatim when I return to the world its completely gone. The old world is gone and no new world is spawned.

avatar image fendorio · Apr 18, 2014 at 08:07 PM 0
Share

Sorry for the late reply. Get rid of either your start or awake?

     static bool spawned = false;
 
     void Start () 
     {
        if(spawned == false)
          {
              spawned = true;
              DontDestroyOnLoad(gameObject);
 
      
              GenerateLandscapeAndObjects(xCubes,zCubes);
              GenerateBorder(xCubes,zCubes);
          }
         else
          {
             DestroyImmediate(gameObject);
          }
     }
    
     void Awake()
     {
        //Leave this empty and try that.
     }
avatar image Razputin · Apr 19, 2014 at 12:19 AM 0
Share

Hey no problem, thanks for offering the help! This still does the same thing though unfortunately. I also tried moving it to awake but it still does it.

avatar image Razputin · Apr 22, 2014 at 03:52 AM 0
Share

I'm still looking for help with this if anyone knows why it won't work :(

avatar image getyour411 · Apr 22, 2014 at 04:29 AM 0
Share

So I think part of your issue is that you creating lots of GameObjects here and it's likely those GameObjects aren't part of the hierarchy attached to this GObj w/ DontDestroy; if it won't wreck your world you could change the instantiated object transform.parent to this GObj and see if you are getting different results.

Show more comments
avatar image
3

Answer by honor0102 · Oct 05, 2021 at 05:36 PM

Pretty old topic and its even answered but i think there is something good to mention and may help someone

DontDestroyOnLoad only works for root game objects or components of a root game object

this means that your gameObject should not have any parent object

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
avatar image
0

Answer by iamsidv · Aug 12, 2014 at 10:47 AM

@Razputin There is a SILLY MISTAKE here...

Let me help you and this will definitely work.

 void Awake()
  {
       if(spawned == false)
          {
               spawned = true;
  
               DontDestroyOnLoad(gameObject);
  
               //Code...
 
                return; //<--- YOU HAVE TO PUT RETURN HERE.
                // BECAUSE once the value is true, it will go in the else condition
                //So we have to stop letting it go in the another condition.
          }
       else
          {
                Destroy(gameObject); //This deletes the new object/s that you
                                              // mentioned were being created
          }
   }


Hope it helps out.

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 meat5000 ♦ · Aug 12, 2014 at 10:49 AM 0
Share

You what now?

avatar image Mosy · Nov 26, 2014 at 08:37 PM 2
Share

You actually would not need the return there. Once you are inside the if statement you can't get into the else statement without rechecking the if statement which would only happen if another gameObject was created.

avatar image
0

Answer by unity_VYAo9PCTYYamFA · Jun 11, 2021 at 11:50 AM

Put it in First line of Awake function it will solve. because it need to execute first

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

27 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

Related Questions

Ground cycle destroy,Ground destruction problem 1 Answer

Not Working properly. Application.LoadLevel 4 Answers

Best way to creating random paths in 2D Grid ? 0 Answers

android application fails to load 0 Answers

How to create multiple ARworldmaps with ARKit using unity3d in a single app? 0 Answers


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