如果语句未运行且没有错误(Unity C#)

时间:2018-11-12 11:20:07

标签: c# if-statement unity3d

所有if语句都没有运行,该怎么办? 所有公共变量都已经分配了一个值。

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

public class OpeningCutscene : MonoBehaviour {
    public int scroll;
    public Text talk;
    public GameObject textS;
    public GameObject player;
    public int banana = 0;
    public int stuff = 1;
    public GameObject smartHELP;
    public GameObject smartRun;
    // Use this for initialization
    void Start () {
        player.SetActive(false);
        this.gameObject.SetActive(false);
        smartRun.SetActive(false);
        this.gameObject.transform.position = new         Vector3(transform.position.x, transform.position.y - 0.9f, transform.position.z);
        textS.SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {
        if (stuff == 1) {
            if (scroll < 300) {
                talk.color = Color.red;
                talk.text = "Hello?";
            } else if (scroll > 300 && scroll < 600) {
                talk.text = "WAKE UP!";
            } else {
                talk.text = "";
                stuff += 1;
            }
            scroll += 1;
            Debug.Log("hi...");

        }
        if (stuff == 2)
        {
            if (banana != 90)
            {
                transform.Rotate(new Vector3(0.0f, 0.0f, -1));
                this.gameObject.transform.position = new Vector3(transform.position.x, transform.position.y + 0.01f, transform.position.z);
                banana += 1;
            }
            else if (banana == 90)
            {
                Debug.Log("Hi");
                stuff = 3;
                player.SetActive(true);
                this.gameObject.SetActive(false);
                smartRun.SetActive(true);
                smartHELP.SetActive(false);
            }
        } 
        if (stuff == 3)
        {
            Debug.Log("hi");
            smartRun.transform.position = new Vector3(smartRun.transform.position.x + 1.0f, smartRun.transform.position.y, smartRun.transform.position.z);

        }
    }

是的,这不是最紧凑的版本,但仍然是

另外,任何人都可以链接到任何我可以用来学习Unity C#的网站,并且必须免费。

1 个答案:

答案 0 :(得分:0)

在Start()方法中初始化您的东西变量。 对您的所有初始化执行此操作。

顺便说一句,如果scroll = 300,那么您的第三个语句将运行:

else {
  talk.text = "";
  stuff += 1;
}

我建议代码重构:)