首页 > 解决方案 > 当 VSCode 没有给我错误时该怎么办?

问题描述

我是 C# 的新手(之前在 python 中做过一些事情),我无法让这段代码工作。我正在制作一个手机游戏,这个脚本应该运行一个计时器,检查计时器是否等于“SaleTime”,以及它是否向用户余额添加资金并将计时器重置为 0。因为 VSCode 没有给我任何错误,我没有知道问题是什么,环顾四周后,我找不到解决方案。

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

public class Sales : MonoBehaviour
{
    public float Timer = 0.0f;
    public float SaleTime = 5.0f;
    public float ProductValue = 5.0f;
    public float Money = 1000.0f;

    void Start()
    {
        StartCoroutine(time());
    }

    public void GameTime()
    {
        Timer += 1;
    }
    IEnumerator time()
    {
        while (true)
        {
            GameTime();
            yield return new WaitForSeconds(1);
        }
    }
    public void SaleFunction()
    {
        if (Timer == SaleTime)
        {
            Timer = 0.0f;
            Money = Money + ProductValue;
        }
    }
}

标签: c#unity3d

解决方案


您没有调用 SaleFunction(),因此程序从不检查条件。

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

public class Sales : MonoBehaviour
{
    public float Timer = 0.0f;
    public float SaleTime = 5.0f;
    public float ProductValue = 5.0f;
    public float Money = 1000.0f;

    void Start()
    {
        StartCoroutine(time());
    }

    public void GameTime()
    {
        Timer += 1;
    }
    IEnumerator time()
    {
        while (true)
        {
            GameTime();
            SaleFunction(); // Added line.
            yield return new WaitForSeconds(1);
        }
    }
    public void SaleFunction()
    {
        if (Timer == SaleTime)
        {
            Timer = 0.0f;
            Money = Money + ProductValue;
        }
    }
}

推荐阅读