The question is answered, right answer was accepted
Why does the instantiation of a prefab work well when the scene is first loaded, but then the cloned GameObjects are no longer displayed in the scene?
I made a GameObject, that instantiates more prefabs of a GameObject the higher the Level is. The first time, it workes excelent, but when you lose the game and it is realoading the scene (with the level, you had bevore), the clones were still in the scene, but you can' t see them. Here is my code for loading the Szene (PlayGame() is called at a ButtonEvent): And here is the code for cloning objects: Can you help me? Thanks in advance!
Answer by streeetwalker · Apr 12, 2020 at 06:36 PM
@Krassomat, when you load a scene everything that you instantiated in the previous scene is destroyed, unless you mark it not to be. It doesn't matter if you are loading the same a scene or a new one.
If you are maintaining level information so that you can instantly jump forward to the level you were at when you "died" after reloading the scene, you need to make sure you execute through all the code that got you to that level. So you must be missing something that you did to get to the level your at in terms of you're objects positions, materials, lighting, rendering or anything that would have to do with their visibility.
If you are doing all that, and you have several levels within a "scene", I'm not sure why you reload the scene at all - it would be better to just reset the level:
You need to keep track of the starting "state" of each level and reset all the variables and object positions and remove anything that changed or got added after the level began. Reset everything back to the level's starting state.
Answer by Krassomat · Apr 13, 2020 at 08:54 AM
Hello, thank you for your answer! But can you tell me how I can delete the duplicatet GameObjects in the Scene?
@ Krassomat, there is no easy answer I can give you because it depends on how you've set up your code and how you add GameObjects objects.
If you use the method I suggested you will restart a Level by returning all the variables to their starting values. Then as Level progresses you will build a List or Array, or Dictionary, that references all GameObjects you've added since the level started. Every time you add a GameObject, you'll add a reference to it to the list
So when you want to restart the Level, fist you Destroy all game objects in that list and then when you restart the Level the list will be recreated from scratch and you will get no duplicates.
I have now done so that the first time the game is played, the GameObjects are not created, but only the second time. What I forgot to tell you last time is that when you finish, the scene is not really reloaded, but only the start menu, which in turn, when you click a button, loads the game scene again. At least it was actually the case with this attempt that although I didn't clone a GameObject the first time, I didn't see it the second time, but was still in the scene. Do you happen to know what this could be or how to fix it? $$anonymous$$any thanks in advance!
@Krassomat, of course the first time you play the game it works.
Well, you cannot load a scene without loading a scene - you cannot load part of a scene. So I don't have any idea what you mean you are not "loading a scene, only the start menu". If you are using the Scene $$anonymous$$anager Class methods to load a scene, then that is what you are doing. It's all or nothing. And again, if you have old objects that still exist after you load a scene, the only way that can happen is if you mark them as DoNotDestroy.
If you are not actually loading a scene, then the problem is that you are not returning all variables to their starting values, and not deleting all objects that you instantiated after you started a level, in order to restart a level.
Without seeing your code there is no way anyone can help further but to offer general strategies.
I'm sorry, I made a mistake. I actually meant when the round was over I loaded another scene (the start menu) and when a button was clicked in that scene the main scene is loaded (with a Click-Event). To give you a better understanding, what I mean, I will give you the Code for it.` (For the $$anonymous$$ain-Scene) void Reaload() { if(gameObject.tag == "Bombe") { Destroy(gameObject); } Scene$$anonymous$$anager.LoadScene("$$anonymous$$enu"); } (For the $$anonymous$$enu-Scene) public void PlayGame() { Scene$$anonymous$$anager.LoadScene("Haubtlevel"); }
And very thanks for your friendly help!
@Krassomat, sorry that doesn't help. You really need to show your complete code, and paste using the Code Sample button - that looks like 101 010 in the tool bar.
Here is my code for the $$anonymous$$ain Scene:
using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; public class GroundCollider : $$anonymous$$onoBehaviour { public GameObject overgame; void Start() { overgame.SetActive(false); } void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Corona") { overgame.SetActive(true); Invoke("Reaload", 2f); } } void Reaload() { Scene$$anonymous$$anager.LoadScene("$$anonymous$$enu"); } }
And here for the $$anonymous$$enu (EndGame() is called a a ButtonEvent:
using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; public class $$anonymous$$enu : $$anonymous$$onoBehaviour { public void PlayGame() { Scene$$anonymous$$anager.LoadScene("Haubtlevel"); } public void Quit() { Application.Quit(); } }
@Krassomat, Unless you post the code using the Insert Code Button so that it comes out in this format, No one is going to help you. I suggest you edit your original question, and do that, and provide all of the code, including the code that builds the level you are having problems with.
using UnityEngine;
using UnityEngine.Scene$$anonymous$$anagement;
public class GroundCollider : $$anonymous$$onoBehaviour {
public GameObject overgame;
void Start() {
overgame.SetActive( false );
}
void OnCollisionEnter( Collision collision ) {
if( collision.gameObject.tag == "Corona" ) {
overgame.SetActive( true ); Invoke( "Reaload", 2f );
}
}
void Reaload() {
Scene$$anonymous$$anager.LoadScene( "$$anonymous$$enu" );
}
}
And then point out the lines of code that have the objects that are getting duplicated.
You must learn how to use this forum's tools to get help!
Follow this Question
Related Questions
Instantiate a Prefab along the surface of a sphere. C# 0 Answers
Destroy attached shield to player (C#) 2 Answers
[C#] Object not spawning where it should? 0 Answers
Instantine - none collider 0 Answers