Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ROOOOCKSTAR · Sep 30, 2014 at 09:29 PM · sceneloadloadlevelinvokerepeating

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"); }
 
     }

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ROOOOCKSTAR · Oct 01, 2014 at 06:04 PM 0
Share

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.

avatar image smoggach · Oct 01, 2014 at 06:10 PM 0
Share

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.

avatar image ado112 · Oct 01, 2014 at 06:12 PM 0
Share

Have you tried something like DontDestroyOnLoad() function?

avatar image ROOOOCKSTAR · Oct 01, 2014 at 06:16 PM 0
Share

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 ?

avatar image ado112 · Oct 01, 2014 at 06:32 PM 0
Share

I also had a game where plenty of objects were spawned then deleted but DontDestoryOnLoad() helped when i wanted to fix some stuff.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Okrim · Jul 31, 2016 at 02:29 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ROOOOCKSTAR · Oct 01, 2014 at 07:55 PM 0
Share

I edited my post.

avatar image ROOOOCKSTAR · Oct 01, 2014 at 08:24 PM 0
Share

Another thing I noticed: the bug doesn't happen while testing on Unity Remote, but only after build (once the application is created).

avatar image smoggach · Oct 02, 2014 at 01:07 PM 0
Share

I see your Asteroids script using Invoke with a delay. Are you forgetting to use CancelInvoke?

avatar image
0

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);
             }
         }
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load / Stream 3rd Party Scene? 1 Answer

unity3D changingscene 0 Answers

Load scene on button press 4 Answers

HELP Game Scene 8 (-1) Cannot be loaded because it has not been added to the build settings 1 Answer

Input.GetMouseButton on multiple objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges