There is a project on unity. The bottom line is that when loading a new (second) level from the prefab, I have to delete the old (first) level with the Level tag. I do it like this:

GameObject[] objects = GameObject.FindGameObjectsWithTag("Level"); foreach (GameObject item in objects) { Destroy(item); Debug.Log("Destroy1"); } objects = GameObject.FindGameObjectsWithTag("Respawn"); foreach (GameObject item in objects) { Destroy(item); Debug.Log("Destroy2"); } hero.level = Instantiate(AssetDatabase.LoadAssetAtPath(string.Format("Assets/Resources/Levels/{0}lvl.prefab",hero.lvl), typeof(GameObject))) as GameObject; hero.level.transform.position = new Vector3(0, 0, -1); Debug.Log(GameObject.FindGameObjectsWithTag("Level").Length); hero.gameObject.transform.position = GameObject.FindWithTag("Respawn").transform.position; 

But, as a result, my hero falls on the first level to the old place, well, that is, to the first Respawn. What to do, I won’t add my mind, for I have already tried almost everything that exists. Thank)

UPD: I would also like to note that the objects themselves are hidden, that is, there really is a feeling that everything has disappeared, but the program does not think so.

    1 answer 1

    Well, for a start - do not use FindWithTag ) it is difficult to find something more unreliable and less productive.

    Create a GameLevel class and hang it on the root object of your levels. Set the fields in this class with links to all the things you need at the level of things - for example, the respawn point (for example, let's call it RespawnPoint ).

    Create a class LevelManager - the path he knows the names of all levels and is able to load and create them in the right place. Let him have a GameLevel type GameLevel , and if at the time of the level change it is not null, the manager destroys the object from it, then loads, creates and assigns a new level to it, and then sponge the hero at the point that the RespawnPoint field refers to. GameLevel .

    Search for objects on the scene through the name or tags you need only in the most extreme case :) otherwise all kinds of unforeseen situations will happen quite often.

    • Gorgeous, thank you) Another such question - I track the level change so that the player falls on the bar below the screen. So in general, the right thing to do?) - piller97
    • @ piller97, If the answer is correct - mark it as correct. And about the add. The question is not quite understood) What do you mean by "tracking the change in the level"? They say he jumps into some kind of pit, does a collider stand below, which catches the contact and starts to load a new level? - M. Green
    • Exactly) I wonder how it’s right) - piller97
    • @ piller97, but it’s quite normal just like tracking. How do you then notify the manager that the level is over? In theory, this “strip” needs to hang some class that will simply be “Handler”, i.e. only catch the event of the player's entry into it and cause an event about it. And, for example, LevelManager or GamaLevel itself will subscribe to the list of these handlers at the start of a new level. Then the class handler itself for anything can then be used - at least for teleporting the hero when entering the zone, at least for throwing up. - M. Green