-3

So I made a game like Happy Glass and it's working fine, but sometimes when the level is completed, the screen to go to the next level appears and then immediately dissapears and u cant press next level

Here it's a picture for reference:

enter image description here

This screen appears and dissapears in a fraction of a second so you cant click to go to the next level.

I have a function that invokes the next level in a particular time, i tried to change the time but didnt work.

The finish line has this code attached to it ( line that makes you go to next level):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GlassFill : MonoBehaviour {

    int trigCont;
    GameManager gm;
    int Star;
    // Use this for initialization
    void Start () {
        gm = FindObjectOfType<GameManager>();
    }

    // Update is called once per frame
    void Update () {

    }
    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "DynamicParticle")
        {
            if (trigCont == 0)
            {
                transform.parent.GetChild(0).GetComponent<SpriteRenderer>().sprite = gm.SurpriseGlass;

            }
            col.gameObject.tag = "InGlassWater";
            col.gameObject.GetComponent<Rigidbody2D>().gravityScale = .3f;
            col.gameObject.GetComponent<Rigidbody2D>().velocity = col.gameObject.GetComponent<Rigidbody2D>().velocity /4;
            trigCont++;

            if (trigCont > 50)
            {
                if (trigCont == 51)
                {
                    if (Mathf.FloorToInt(gm.PenCapacity.value * 100) > 75)
                    {
                        Star = 3;
                    }
                    else if (Mathf.FloorToInt(gm.PenCapacity.value * 100) > 50)
                    {
                        Star = 2;
                    }
                    else if (Mathf.FloorToInt(gm.PenCapacity.value * 100) > 25)
                    {
                        Star = 1;
                    }
                    print(Star + "star");
                    transform.parent.GetChild(0).GetComponent<SpriteRenderer>().sprite = gm.HappyGlass;
                    Camera.main.GetComponent<AudioSource>().Play();
                    Invoke("nextScene", 0);
                    CancelInvoke("Check");
                    for (int i = 0; i < Camera.main.transform.childCount; i++)
                    {
                        if (Camera.main.transform.GetChild(i).GetComponent<ParticleSystem>() != null)
                        {
                            Camera.main.transform.GetChild(i).GetComponent<ParticleSystem>().Play();
                        }
                    }
                }               
            }
            else
            {
                CancelInvoke("Check");
                Invoke("Check",5);
            }
            if (trigCont > 60)
            {                                               //You can write  a function over here if you want to give a star according to glass fill
                print("two star");
            }           
            if (trigCont > 70)
            {
                print("three star");
            }
        }
    }
    void Check()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
    void nextScene()
    {
        PlayerPrefs.SetInt((SceneManager.GetActiveScene().buildIndex).ToString(), 1);
        PlayerPrefs.SetInt("Star" + SceneManager.GetActiveScene().name, Star);
        gm.LevComp.SetActive(true);
        if (Star > 2)
        {
            gm.LevComp.transform.GetChild(0).gameObject.SetActive(true);
            gm.LevComp.transform.GetChild(1).gameObject.SetActive(true);
            gm.LevComp.transform.GetChild(2).gameObject.SetActive(true);
        } 
        else if (Star > 1)
        {
            gm.LevComp.transform.GetChild(0).gameObject.SetActive(true);
            gm.LevComp.transform.GetChild(1).gameObject.SetActive(true);
        }
        else if (Star>0)
        {
            gm.LevComp.transform.GetChild(0).gameObject.SetActive(true);
        }
    }
}

Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
Filu
  • 45
  • 6
  • Id add some debug messages sounds like something like the restart level and next level is trying to happen at the same time – BugFinder May 24 '19 at 11:06
  • 1
    Why are you calling `Invoke("nextScene", 0);`? That function both: (a) exists in the same context and you're saying "call it immediately" (why not just `nextScene()`?) and (b) doesn't actually go to the next scene. – Draco18s no longer trusts SE May 24 '19 at 15:28

1 Answers1

1

As per your code, I believe the level is completed once trigCont > 50, and in that moment you display a menu with the number of starts and with the buttons to repeat the level or move to the next one.

If that is the case you should move Invoke("nextScene", 0); to the end of that condition. Something like

if (trigCont > 50)
{
    if (trigCont == 51)
    {
        //...          


        for (int i = 0; i < Camera.main.transform.childCount; i++)
        {
            if (Camera.main.transform.GetChild(i).GetComponent<ParticleSystem>() != null)
            {
                Camera.main.transform.GetChild(i).GetComponent<ParticleSystem>().Play();
            }
        }
        CancelInvoke("Check");
        Invoke("nextScene", 0);
    }               
}

Also since that suppose to be the end of the level, I would add a flag just at the beginning of void OnTriggerEnter2D(Collider2D col) to ignore anything else happening in the scene. The best would be to stop/disable/destroy anything else in the scene. But I don't have information about what other elements may be interacting in your scene. So you can do:

int trigCont;
GameManager gm;
int Star;
bool sceneIsOver = false;
...

And the inside OnTriggerEnter2D:

void OnTriggerEnter2D(Collider2D col)
{
    if(!sceneIsOver)
        if (col.gameObject.tag == "DynamicParticle")
        {
            //...

            if (trigCont > 50)
            {
                if (trigCont == 51)
                {
                    //...

                    for (int i = 0; i < Camera.main.transform.childCount; i++)
                    {
                        if (Camera.main.transform.GetChild(i).GetComponent<ParticleSystem>() != null)
                        {
                            Camera.main.transform.GetChild(i).GetComponent<ParticleSystem>().Play();
                        }
                    }
                    CancelInvoke("Check");
                    sceneIsOver = true;
                    Invoke("nextScene", 0);
                }               
            }
            //...
        }
    }
}
Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94