- Home /
Scene not loading properly everytime
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.
Try 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.
Have you tried something like DontDestroyOnLoad() function?
Well 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 ?
I also had a game where plenty of objects were spawned then deleted but DontDestoryOnLoad() helped when i wanted to fix some stuff.
Answer by ROOOOCKSTAR · Oct 06, 2014 at 01:36 PM
It seemed I figured out what is wrong (but I don't why). It looks like if I don't destroy manually the particles still alive on the scene before loading the next scene, then the load will be screwed up.
So far, the best thing I found is to destroy every particle remaining in the scene manually (FindGameObjectswithtag "particles" for example).
Is it a normal behaviour ? Thanks.
Thank you, I had the same issue and the particles were indeed the ones that caused it.
Load up the new scene before they show up did the trick.
Answer by KMKxJOEY1 · Oct 01, 2014 at 06:14 PM
Something that happens in one scene should not affect how a separate scene is loaded; you probably have another problem. Post some code.
Another thing I noticed: the bug doesn't happen while testing on Unity Remote, but only after build (once the application is created).
I see your Asteroids script using Invoke with a delay. Are you forgetting to use CancelInvoke?
Answer by Niloy · Jul 31, 2016 at 03:08 PM
I have a Particle Destroy Code that might help you. Attach this script to every of your Particles. They will get automatically destroyed after finishing particle process.
using UnityEngine;
using System.Collections;
public class Kill_Explosion : MonoBehaviour {
private ParticleSystem ps;
// Use this for initialization
void Start () {
ps = GetComponent<ParticleSystem>();
}
// Update is called once per frame
void Update () {
if(ps)
{
if(!ps.IsAlive())
{
Destroy(gameObject);
}
}
}
}