【问题标题】:If statements aren't running and has no errors (Unity C#)如果语句没有运行并且没有错误(Unity C#)
【发布时间】:2018-11-12 11:20:06
【问题描述】:

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#(如 codeacademy)并且必须是免费的任何网站。

【问题讨论】:

  • 是什么让您相信if 语句没有运行?
  • 你调试过这个,看看stuff是什么?我猜它是零。
  • 顺便说一句,stuff 是一个可怕的变量名!
  • 场景中对应的 GaneObject 是否处于活动状态且组件是否启用?
  • 除了 .. 每次只检查一个变量的不同值时使用switch

标签: c# if-statement unity3d


【解决方案1】:

在 Start() 方法中初始化你的 stuff 变量。 对所有初始化都这样做。

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

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

我建议进行代码重构:)

【讨论】:

  • 他为什么要在 Start() 中初始化他的公共字段的值?这样他所有来自 Inspector 的配置都会被覆盖..!
  • 你是什么意思(我不擅长编码(初学者))我理解 else 问题,但问题是 if 语句甚至没有运行。
  • @ThomasHilbert,我不是指从编辑器初始化的值,而是像香蕉这样的东西。挖掘机,我很确定变量的东西在你的游戏开始时没有价值。因此,请尝试在 Unity 提供的 Start() 方法中对其进行初始化。无效开始(){东西= 1;除此之外,在 Update() 方法中,当比较相同的变量时,在你的情况下是“东西”,使用 if-else 而不是 if-if 语句。 if-else 进行排他性检查,而 if-if 进行所有检查。
  • @NikosGiannaras 也 if-else 效率不高,因为它仍然会经过所有检查,直到一个条件适用。最好在这里使用switch-casestuffbananapublic,因此将出现在 Inspector 中,并且可能具有与脚本中不同的起始值。在这种情况下,OP 可能不希望在 Start 中覆盖它。
  • 我发现了问题(未激活),但最后一个条件语句不会加载。即使东西 = 3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-28
  • 2012-09-16
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
相关资源
最近更新 更多