Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 分数条件在我的代码中不起作用_C#_Android_If Statement_Unity3d - Fatal编程技术网

C# 分数条件在我的代码中不起作用

C# 分数条件在我的代码中不起作用,c#,android,if-statement,unity3d,C#,Android,If Statement,Unity3d,我正在unity 3d中制作一个flappy bird游戏,但当我对分数设置的条件大于3时,Application.LoadLevel()在if语句中不起作用。 我还放了Debug.Log来检查if语句,但它也不起作用。 我已经附上下面的代码,你可以看到它 using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Scor

我正在unity 3d中制作一个flappy bird游戏,但当我对分数设置的条件大于3时,Application.LoadLevel()在if语句中不起作用。 我还放了Debug.Log来检查if语句,但它也不起作用。 我已经附上下面的代码,你可以看到它

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


public class Score : MonoBehaviour {

static int score = 0;
static Score instance;
Text text;

static public void AddPoint() 
{ 
    if (score > 3) 
    {
        SceneManager.LoadScene ("MainMenu");
        if (instance.bird.dead)  return;
    }
    else
    {               
        SceneManager.LoadScene( Application.loadedLevel );
    }

    score++;
}

BirdMovement bird;

void Start() 
{
    instance = this;
    GameObject player_go = GameObject.FindGameObjectWithTag("Player");

    if(player_go == null) 
    {
        Debug.LogError("Could not find an object with tag 'Player'.");
    }   

    bird = player_go.GetComponent<BirdMovement>();
}

void OnDestroy() 
{
    instance = null;
}

void Awake ()
{
    // Set up the reference.
    text = GetComponent<Text> ();

    // Reset the score.
    score =0;
}

void Update () 
{
    text.text = ""+ score;
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.UI;
使用UnityEngine.SceneManagement;
公共课成绩:单一行为{
静态智力得分=0;
静态评分实例;
文本;
静态公共void AddPoint()
{ 
如果(分数>3)
{
SceneManager.LoadScene(“主菜单”);
如果(例如,bird.dead)返回;
}
其他的
{               
SceneManager.LoadScene(Application.loadedLevel);
}
分数++;
}
鸟移鸟;
void Start()
{
实例=此;
GameObject player_go=GameObject.FindGameObjectWithTag(“player”);
if(player_go==null)
{
LogError(“找不到标记为“Player”的对象”);
}   
bird=player_go.GetComponent();
}
void OnDestroy()
{
实例=null;
}
无效唤醒()
{
//设置引用。
text=GetComponent();
//重设分数。
得分=0;
}
无效更新()
{
text.text=”“+分数;
}
}
我是个新手,请帮帮我。
谢谢

据我所知,您永远不会更改
分数的值。默认情况下,它是
0
,因此
分数
永远不会高于
3

当分数大于3时,您正在设置条件,但如果满足后条件,则使用另一个条件。首先清除此项并编辑您的问题

应用程序。不推荐使用LoadLevel

使用

不要忘记使用UnityEngine.SceneManagement

你的球员必须做什么才能赢得1分?在哪里调用“AddPoint()”

编辑

static public void AddPoint()
        { 
        // First add score point
        score++;

        // When score better than 3 go to Main Menu
        if (score > 3) {
            SceneManager.LoadScene ("MainMenu");
        }

        // Another condidion
        if (instance.bird.dead) { 
            return;

        } else {

            SceneManager.LoadScene ("What_level_you_want");
        }
    }

还可以使用SceneManager切换场景

SceneManager.LoadScene ("Your NextScene Name");

请使用UnityEngine.SceneManagement实现命名空间:

错误在if语句的范围内。您的代码所做的是:如果分数大于3,则增加分数。即使分数不大于3,也必须在if子句之外才能增加。我想代码现在已更新,请单击编辑并单击f或者正确地编写代码,没有浪费的换行符,thanksBro SceneManager.LoadScene不起作用。命名空间“UnityEngine”中不存在SceneManagement。是否缺少程序集引用?如我所说,是使用UnityEngine导入的,
。场景管理
?别忘了!是的,我导入了SceneManager.LoadScene不起作用。我的unity版本是4.6.1,因此必须使用Application.LoadLevel(0);
Application.LoadLevel(<levelNumber>)
Application.LoadLevel(0);
SceneManager.LoadScene ("Your NextScene Name");