Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity 3d场景正在加载/未加载_C#_Unity3d - Fatal编程技术网

C# Unity 3d场景正在加载/未加载

C# Unity 3d场景正在加载/未加载,c#,unity3d,C#,Unity3d,我的场景不断地说是加载和未加载,我不能正常播放 我没有收到任何错误消息或警告。我不知道代码中是否有错误如果是的,我不知道在这里导入哪个部分,因为它太长了,我不认为这是一件常见的事情,因为我在论坛中找不到与此问题相关的任何内容。 如果有办法解决它,请帮助。 在我将此添加到更新功能之前,它工作正常: if (health <= 0) { RestartGame(); } 我以前有过这样的经历: void RestartGame() { StartCoroutine(Wait3

我的场景不断地说是加载和未加载,我不能正常播放

我没有收到任何错误消息或警告。我不知道代码中是否有错误如果是的,我不知道在这里导入哪个部分,因为它太长了,我不认为这是一件常见的事情,因为我在论坛中找不到与此问题相关的任何内容。 如果有办法解决它,请帮助。 在我将此添加到更新功能之前,它工作正常:

if (health <= 0)
{
    RestartGame();
}
我以前有过这样的经历:

void RestartGame()
{
    StartCoroutine(Wait3secs());
    Application.LoadLevel(Application.loadedLevel);
}
完整代码:

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

你的健康指数为0;在开始时,如果health则health=0;在你开始的时候,如果我真的不明白怎么了,你的黄色加载指示器或者你的游戏甚至没有开始?From:这已经过时了。另请注意StartRoutineWait3Ses;运行例程,但不等待其结果。。它会立即转到下一行,请您出示完整的代码。。。声明您将此添加到void OnControllerColliderHit ControllerColliderHit c对我们来说毫无意义,因为我们看不到OnControllerColliderHit的其余部分以及您的代码段是如何连接的为什么您的更新方法被标记为[System.Observe]?我真的不明白到底是怎么回事,您的黄色加载指示器或您的游戏甚至没有启动?发件人:这现在已过时。另请注意StartRoutineWait3Ses;运行例程,但不等待其结果。。它会立即转到下一行,请您出示完整的代码。。。声明您将此添加到void OnControllerColliderHit ControllerColliderHit c对我们来说毫无意义,因为我们看不到OnControllerColliderHit的其余部分以及您的代码段是如何连接的为什么您的更新方法被标记为[System.Observe]?感谢@derHugo的编辑,我在工作中很匆忙地写了一篇文章:PThanks for the edit@derHugo,在工作中匆忙写作:P
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);
    }
}