1

My scene continuously says is loading and not loaded and I can not play properly

image1

image2

I don't get any error message or warning. I don't know if there is a mistake in the code (if yes, I don't know which part to import here because it is too long), I don't think this is a common thing because I couldn't find anything in the forums related to this issue. If there is a way to fix it please help. It was working fine till I added this to Update() function:

if (health <= 0)
{
    RestartGame();
}

And added this to void OnControllerColliderHit (ControllerColliderHit c)

if (c.gameObject.tag == "enemy")
{
    AudioSource audio1 = GetComponent<AudioSource>();
    audio1.PlayOneShot(hurt);
    health -= 25; 
}

I had this before :

void RestartGame()
{
    StartCoroutine(Wait3secs());
    Application.LoadLevel(Application.loadedLevel);
}

Complete Code:

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

public class detect_collision : MonoBehaviour
{
    private int health;
    public bool  isGun;
    private bool  isKey;
    private bool isMatches;
    private int usedGun = 0;
    public AudioClip soundCollectedObject;
    public AudioClip noammo;
    public AudioClip gameover;
    public AudioClip win;
    public AudioClip hurt;
    //public AudioClip fireSound;
    public GameObject sight;
    public GameObject bulletcount;
    private bool showHint = false;
    private float timer = 0;
    public float displayTime = 5.0f;
    public Text hintGUI;
    public GameObject campfire;
    public ParticleSystem smokeSystem;
    public ParticleSystem fireSystem;
    int fireEmit;



    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1;
        isKey=false;
        isGun=false;
        isMatches = false;
        fireEmit = 0;
        health=0;
        changeTexture("key", isKey);
        changeTexture("gun", isGun);
        changeTexture("matches", isMatches);
        GameObject.Find("UI_sight").GetComponent<fire>().enabled = false;
        GameObject.Find("UI_texture_gun").GetComponent<RawImage>().enabled = false;
        sight.SetActive(false);
        bulletcount.SetActive(false);
    }

    // Update is called once per frame
    [System.Obsolete]
    void Update()
    {
        smokeSystem.Emit(fireEmit);
        fireSystem.Emit(fireEmit);

        AudioSource audio = GetComponent<AudioSource>();
        if (Input.GetButtonDown("Fire1"))
        {
            if (usedGun == 0)
            {
                if (GameObject.Find("UI_texture_gun").GetComponent<RawImage>().enabled == true)
                {
                    int bullet = GameObject.Find("UI_sight").GetComponent<fire>().bullets;
                    if (bullet <= 0)
                    {
                        audio.PlayOneShot(noammo);
                        usedGun++;
                        isGun = false;
                    }
                }
            }
            else
            {
                audio.PlayOneShot(noammo);
            }
        }

        if (showHint)
        {
            if (timer < displayTime)
            {
                timer += Time.deltaTime;
            }
            else
            {
                hintGUI.enabled = false;
                showHint = false;
                timer = 0;
            }
        }

        if (health <= 0)
        {
            RestartGame();
        }
    }


    void showHintGUI(string txt)
    {
        hintGUI.text = txt;
        hintGUI.enabled = true;
        this.showHint = true;
    }

    void  changeTexture (string obj,bool show)
    {
        GameObject.Find("UI_texture_" + obj).GetComponent<RawImage>().enabled = show;
    }


    void  OnControllerColliderHit (ControllerColliderHit c)
    {
        if (c.gameObject.tag == "firstAidKit")
        {
            health = 100;
            GameObject.Find("health_bar").GetComponent<health_bar>().setHealth(health);
            GameObject.Find("UI_message_for_user").GetComponent<message_for_user>().showText(c.gameObject.tag + " collected!");
            Destroy(c.gameObject);
            AudioSource audio = GetComponent<AudioSource>();
            audio.clip = soundCollectedObject;
            audio.Play();
        }
        if (c.gameObject.tag == "gun")
        {
            isGun=true;
            changeTexture("gun", isGun);
            GameObject.Find("UI_message_for_user").GetComponent<message_for_user>().showText(c.gameObject.tag + " collected!");
            Destroy(c.gameObject);
            AudioSource audio = GetComponent<AudioSource>();
            audio.clip = soundCollectedObject;
            audio.Play();
            usedGun = 0;
            sight.SetActive(true);
            GameObject.Find("UI_sight").GetComponent<fire>().bullets = 20;
            bulletcount.SetActive(true);
            GameObject.Find("UI_bullet_count").GetComponent<UnityEngine.UI.Text>().text = "Bullets: " + GameObject.Find("UI_sight").GetComponent<fire>().bullets + "/20";
            GameObject.Find("UI_sight").GetComponent<fire>().enabled = true;
        }
        if (c.gameObject.tag == "key")
        {
            isKey=true;
            changeTexture("key", isKey);
            GameObject.Find("UI_message_for_user").GetComponent<message_for_user>().showText(c.gameObject.tag + " collected!");
            Destroy(c.gameObject);
            AudioSource audio = GetComponent<AudioSource>();
            audio.clip = soundCollectedObject;
            audio.Play();
        }
        if (c.gameObject.tag == "fiery")
        {
            GameObject.Find("UI_message_for_user").GetComponent<message_for_user>().showText("GAME OVER!");
            Destroy(c.gameObject);
            AudioSource audio = GetComponent<AudioSource>();
            audio.PlayOneShot(gameover);
            Time.timeScale = 0;
            RestartGame();
        }
        if (c.gameObject.tag == "door")
        {
            if (isKey)
            {
                GameObject.Find("UI_message_for_user").GetComponent<message_for_user>().showText("You have completed \n the game level!");
                //Destroy(c.gameObject);
                AudioSource audio = GetComponent<AudioSource>();
                audio.PlayOneShot(win);
                StartCoroutine(WaitForIt(1.0F));
            }
        }
        if (c.gameObject.tag == "matches")
        {
            isMatches = true;
            changeTexture("matches", isMatches);
            GameObject.Find("UI_message_for_user").GetComponent<message_for_user>().showText(c.gameObject.tag + " collected!");
            Destroy(c.gameObject);
            AudioSource audio = GetComponent<AudioSource>();
            audio.clip = soundCollectedObject;
            audio.Play();
        }
        if (c.gameObject.tag == "fire")
        {
            if(isMatches)
            {
                if(Input.GetMouseButtonDown(0))
                {
                    lightFire(campfire);
                    isMatches = false;
                    changeTexture("matches", isMatches);
                }
            }
        }
        if (c.gameObject.tag == "enemy")
        {
            AudioSource audio1 = GetComponent<AudioSource>();
            audio1.PlayOneShot(hurt);
            health -= 25; 
        }
    }

    void lightFire( GameObject cmpfire)
    {
        fireEmit = 1;
        cmpfire.GetComponent<AudioSource>().Play();
        //AudioSource audio = GetComponent<AudioSource>();
        //audio.clip = fireSound;
        //audio.playOnAwake = true;
    }

    IEnumerator WaitForIt(float waitTime)
    {
        yield return new WaitForSeconds(waitTime);
        SceneManager.LoadScene("scene02");
    }

    void RestartGame()
    {
        StartCoroutine(Wait3secs());
        SceneManager.LoadScene("SampleScene");
    }

    IEnumerator Wait3secs()
    {
        yield return new WaitForSecondsRealtime(3f);
    }
}
9
  • I don't really get it what is wrong, your yellow loading indicator or that your game don't even start?
    – Menyus777
    Commented Dec 20, 2019 at 12:37
  • 1
    From Application.LoadLevel: This is now obsolete. Use SceneManager.LoadScene instead.
    – derHugo
    Commented Dec 20, 2019 at 12:43
  • 1
    Also note that StartCoroutine(Wait3secs()); runs a routine but does not wait for its outcome .. it immediately goes to the next line
    – derHugo
    Commented Dec 20, 2019 at 12:45
  • Could you please show a complete code ... stating that you added this to void OnControllerColliderHit (ControllerColliderHit c) means nothing to us since we don't see the rest of OnControllerColliderHit and how your code snippets are connected
    – derHugo
    Commented Dec 20, 2019 at 12:46
  • 1
    why is your Update method tagged [System.Obsolete]?
    – derHugo
    Commented Dec 20, 2019 at 13:23

1 Answer 1

5

You have health = 0; in your Start(), and if(health<=0) RestartGame(); in your Update(), so your game is always restarting.

Try not to start the game with health = 0 and that's all I think.

1
  • 1
    Thanks for the edit @derHugo, was writing in a hurry at work :P
    – Windgate
    Commented Dec 20, 2019 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.