当请求下一个场景不加载时

时间:2019-05-19 16:17:00

标签: c# unity3d

我创建了一个游戏,当用户打破所有障碍时,他会被带到下一个场景,尽管添加了我在构建设置中拥有的所有场景,但这并没有发生。我没有任何错误,场景编写正确。有人可以帮我解决这个问题吗?

这是构建设置  enter image description here

Bricks脚本:(调用场景的地方)

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

public class Bricks : MonoBehaviour {

public LevelManager myLevelManager;
public static int brickCount = 0;
public int maxNumberOfHits = 0;
int timesHit;
public AudioClip BlockBreaking;

// Use this for initialization
void Start () {

    timesHit = 0;

    if(this.gameObject.tag == "BrickHit")
    {
        brickCount++;

    }

    if(this.gameObject.tag == "BrickHitTwice")
    {
        brickCount++;
    }
}

void OnCollisionEnter2D()
{
    timesHit++;

    if (timesHit == maxNumberOfHits)
    {
        brickCount--;
        Destroy(this.gameObject); 
    }

    if(brickCount == 0)
    {
        myLevelManager.LoadLevel("Level1.2"); //THIS SCENE IS NOT LOADING
    }

    if(this.gameObject.tag == "BrickHit") //If the gameObject (Block One Point) with the tag "BrickHit" is hit
    {
        Scores.scoreValue += 1;//The user will be rewarded 1 point
        AudioSource.PlayClipAtPoint(BlockBreaking, transform.position);
    }

    if(this.gameObject.tag == "BrickHitTwice") //If the gameObject (Block Two Points) with the tag "BrickHitTwice" is hit
    {
        Scores.scoreValue += 2; //The user will be rewarded 2 points
        AudioSource.PlayClipAtPoint(BlockBreaking, transform.position);
    }   
}

LevelManager脚本:

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

public class LevelManager : MonoBehaviour {
    public void LoadLevel(string name)
    {
       print("Level loading requested for" + name);
       SceneManager.LoadScene(name);
    }

2 个答案:

答案 0 :(得分:0)

我怀疑您的错误可能在于您在Destroy()游戏对象可以加载下一个场景之前对其进行了设置;您将获得竞赛条件,了解首先完成的比赛; LoadScene或Destroy()-可以解释为什么有时起作用。在理解您的问题之前,您永远不要以为这是框架中的错误。

尝试将Destroy()放在LoadScene()之后,或稍稍延迟一下以了解这是否是您的问题。


此外,您的LevelManager可以设置为静态的,并且不需要继承自MonoBehaviour,因为它不使用gameObject功能。

public static class LevelManager {
    public static void LoadLevel(string name)
    {
       print("Level loading requested for" + name);
       SceneManager.LoadScene(name);
    }
}

通过LevelManager.LoadLevel("MyLevel");来使用,但是随后您可能会问,使用LevelManager.LoadLevel或SceneManager.LoadLevel会更有效,因为它们将做完全相同的事情。

答案 1 :(得分:0)

您遇到的主要问题是没有一个来源来检查brickCount,而是每个单独的积木都在维护自己的计数。我建议将积木计数逻辑移到一个单独的类中。 LevelManager似乎是个好地方。因此,在LevelManager中添加:

private int brickCount = 0;
public void AddBrick()
{
    brickCount++;
}
public void RemoveBrick()
{
    brickCount--;
    // Check if all bricks are destroyed
    if (brickCount == 0)
    {
        LoadLevel("Level1.2");
    }
}

然后在您的积木脚本中:

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

public class Bricks : MonoBehaviour {
    public LevelManager myLevelManager;        
    public int maxNumberOfHits = 0;
    int timesHit;
    public AudioClip BlockBreaking;

    // Use this for initialization
    void Start () {
        timesHit = 0;
        myLevelManager.AddBrick();
        // I'm not sure why you were checking the tag here, since the result was the same  
    }

    void OnCollisionEnter2D()
    {
        timesHit++;

        if (timesHit == maxNumberOfHits)
        {
            myLevelManager.RemoveBrick();
            Destroy(this.gameObject); 
        }
        /* This looks like the player is getting score whether the brick is destroyed or not. Also, it would appear the player won't get scored on the final brick */
        if(this.gameObject.tag == "BrickHit") //If the gameObject (Block One Point) with the tag "BrickHit" is hit
        {
            Scores.scoreValue += 1;//The user will be rewarded 1 point
            AudioSource.PlayClipAtPoint(BlockBreaking, transform.position);
        }

        if(this.gameObject.tag == "BrickHitTwice") //If the gameObject (Block Two Points) with the tag "BrickHitTwice" is hit
        {
            Scores.scoreValue += 2; //The user will be rewarded 2 points
            AudioSource.PlayClipAtPoint(BlockBreaking, transform.position);
        }   
    }
}