Hi, I’m doing a 2D game for Android.
I have a first scene having 1 button.
The second scene is the game itself, a lot of objects loaded, then destroyed, interactions, etc…
I configured the “Escape” button on this scene to go back to the first scene when pressed.
There are enemies spawning randomly.
However, it seems like when I press escape to go back to the first scene, nothing is loaded (I see blue screen like there is nothing) sometimes. After doing some tests, I realized that, if on the second scene, one or more enemies are spawned, if I press the button, then nothing is loaded on scene 1. If I deactivate Enemies (delete InvokeRepeate, or deactivate the prefabs), then it works well everytime.
So my question is, what thing in a scene can prevent another one to load properly ? Thanks.
EDIT:
Okay so first I have my button in the menu, when you press it you Load the TEST scene (the game):
void Update () {
if (Input.touchCount > 0) {
for (int i = 0; i < Input.touchCount; i++) {
Vector3 wp = Camera.main.ScreenToWorldPoint (Input.GetTouch (i).position);
Vector2 touchPos1 = new Vector2 (wp.x-0.5f, wp.y-0.5f);
Vector2 touchPos2 = new Vector2 (wp.x+0.5f, wp.y+0.5f);
if (this.collider2D == Physics2D.OverlapArea(touchPos1,touchPos2)) {
if (Input.GetTouch (i).phase == TouchPhase.Began)
{
Application.LoadLevel("test");
}
}
}
}
}
Now on the game scene I have a Invoker script, random values are used for random spawning:
public class InvokeEnemies : MonoBehaviour {
public GameObject Enemies;
public GameObject Asteroids;
private float randomx;
private float randomy;
void Start () {
InvokeRepeating ("CreateAsteroids", 2f, 5f);
}
void CreateAsteroids()
{
float randoma = 0;
float randoma2 = 0;
int area = Random.Range (0, 3);
switch (area) {
case 0:
{
randoma = Random.Range(-8f,8f);
randoma2 = Random.Range(-5f,-6f);
break;
}
case 1:
{
randoma = Random.Range(-8f,-9f);
randoma2 = Random.Range(-5f,5f);
break;
}
case 2:
{
randoma = Random.Range(-8f,8f);
randoma2 = Random.Range(5f,6f);
break;
}
case 3:
{
randoma = Random.Range(8f,9f);
randoma2 = Random.Range(-5f,5f);
break;
}
}
Instantiate (Asteroids, new Vector3 (randoma, randoma2, 6f), Quaternion.identity);
}
}
Here is the asteroids code:
public class AsteroidOnTouch : MonoBehaviour {
private DontDestroyParticles myscript;
public ParticleSystem explosion;
private ParticleSystem explode;
private int touchCount;
// Use this for initialization
void Start () {
touchCount = 3;
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0) {
for (int i = 0; i < Input.touchCount; i++) {
Vector3 wp = Camera.main.ScreenToWorldPoint (Input.GetTouch (i).position);
Vector2 touchPos1 = new Vector2 (wp.x-0.5f, wp.y-0.5f);
Vector2 touchPos2 = new Vector2 (wp.x+0.5f, wp.y+0.5f);
if (this.collider2D == Physics2D.OverlapArea(touchPos1,touchPos2)) {
if (Input.GetTouch (i).phase == TouchPhase.Began)
{
if (touchCount == 0)
{
myscript = GetComponentInChildren<DontDestroyParticles>();
myscript.DetachParticles();
explode = Instantiate (explosion,transform.position,Quaternion.identity) as ParticleSystem;
Destroy (explode.gameObject,5);
Destroy (this.gameObject);
}
else
{
touchCount--;
}
}
}
}
} else {
}
}
}
When I disable Asteroids, then loading is always good. When there is at least one, it doesn’t work. I just set the escape button to go back to the scene Menu when pressed.
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)) { Application.LoadLevel("menu"); }
}
I'm still looking for an answer. Could it be something in relation with InvokeRepeating ? Destroy(object,timer) ? Cause the doc says that every object is deleted when loading a new scene so I don't how can this interfers with the other scene.
– ROOOOCKSTARTry removing your Destroy. Whenever I try to use this unity tells me it's a bad idea (and it is!). I think maybe it's deleting the assets.
– smoggachHave you tried something like DontDestroyOnLoad() function?
– ado112Well as I said, the transition is between the Game scene -> the menu. During the game there might be some objects still alive when the player wants to go back to the menu, but normally the Application.LoadLevel should destroy all the remaining objects. If I disable the enemies only (objects moving and spawning randomly), I have no problem, the menu is correctly loaded. Why would I use DontDestroyOnLoad() ? The menu needs to be deleted when I load my game scene, right ?
– ROOOOCKSTARI also had a game where plenty of objects were spawned then deleted but DontDestoryOnLoad() helped when i wanted to fix some stuff.
– ado112