If you like a challenge, check this one out (Problems with multi-level loading)

Hello.
I’m working on a Unity project for a home decorator application and I’m facing some difficulties.
The application has 4 different levels:

  1. On the first one you select the distribution for the house (number of rooms, placement of walls, etc.),
  2. On the second level you select the types of windows, doors, etc. you want in your house.
  3. On the 3rd level you apply different textures to walls, ceilings and floors,.
  4. And finally, on level 4, you can add furniture and move it, rotate it, change its color, etc.

Basic stuff.

The problem is the following:
I’d like for the user to be able to go back and forth within all the levels, so that you may go back to level 2 from level 4 to change the windows and doors, and then be able to continue adding new furniture on level 4, or go back to level 1 to delete some walls and then go back to levels 3 or 4, etc. All of this while preserving all the changes you do in the different levels (furnitures should not be destroyed when switching levels, wall textures should be kept the same, etc.)

The issue is that I have a 3D model of the house, which has an attached script that does some fading animations to the walls. In order to preserve the 3D object throughout the application, I created a script called “Indestructible” that basically uses the DontDestroyOnLoad function on the awake method, to preserve the GameObject, its applied textures, etc. Applying that script to the 3D object keeps the model “alive” throughout the application. But as many of you are probably aware, if I load the first level a second time (i.e: the user goes back to the 1st level), the 3D object is loaded again and a clone is created. To prevent this, I created a MySingleton script that destroys any duplicate 3D object created in the first level of the app (since there are a total of 87 mesh objects, I created a counter in the MySingleton script. On the first time the 1st level is loaded, it “stores” the 3D GameObjects in an array, so that when the user returns to the 1st level a second time the array will be full, and will destroy any duplicates of the 3D objects that the “DontDestroyOnLoad” function would incur).

The problem is, when I reach the 4th level, if I go back to levels 1, 2 or 3, the 3D model disappears. In the editor I can still see the object name (I’m guessing this means it wasn’t destroyed. In fact, when you click on each game object that represents a 3D mesh, you can see its wireframe on the Scene window in Unity, but it’s invisible on the Game window), and the object properties tell me that the Mesh Renderer is still enabled, so I have NO idea why the 3D object disappears.

Anyone know what I might be doing something wrong? If you think I should use a different approach to use the 3D objects throughout the application, I’m open to suggestions. Let me know if you’d like me to post any code from my application that you think might be useful to understand how things work.

Cheers.

instead of deleting the new created objects, perform a check if the object exists in the scene before you call a DontDestroyOnload , if it doesn’t call the DontDestroyOnload function and don’t if it already exists.

Thanks for your reply appels.

I thought about that method but, would you try and find if there are other objects with the same gameObject name? (all the 3D meshes have different names. There are no 3D objects that share the same name)

I tried using GameObject.Find(this.gameObject.name), but I think the Find function returns just the first object it finds with that name, so it will always return the same game object and create clones nonetheless.
Should I try using FindObjectsWithTag instead and tagging all the gameobjects with 3D meshes, and if it finds 2 objets with the same tag and name, not use DontDestroyOnLoad?

you can do lots of things, have a look at the GameObject.Find command in the script ref but here is one :
GameObject[ ] gos = GameObject.FindGameObjectsWithTag (“MyTagOnObjects”);
gos = array of gameobjects so you can loop through them.
you can also search them by name if you want.
You can also add objects to an array and do a search based on the objects in the array.

Personally, I would just create the house in a separate level ‘0’:

level 0: create the house
level 1-4: perform the various modification operations

allow free travel between levels 1-4, but never to 0.

I followed your advice tomvds and now I have a level 0 that creates the 3D meshes, and levels 1-4 are for the sections that I described on my first post.

That makes the MySingleton and Indestructible scripts much simpler (and prettier :P), so thanks for the tip.

However, I’m still facing that problem that, whenever I reach level 4, if I go back to any of the previous levels, the 3D models just disappear for some weird reason. :S