c# - 当 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;
}
}
}
解决方案
您没有调用 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;
}
}
}
推荐阅读
- php - 在 CQRS 应用程序中执行验证/不变量
- python - ModuleNotFoundError:没有名为“PyQt5.QtWebKit”的模块
- javascript - 在html中的div中显示文本
- javascript - java - 如何在Java Web应用程序中的页面刷新后保留选定的单选按钮
- .htaccess - 在网站上存储敏感图像的最安全方法是什么?
- javascript - 从 JSON 中调用对象的值时,为什么必须用引号和加号括起来?
- javascript - php没有通过$_POST接收变量
- javascript - 无法从 getElementsByClassName 访问 HTMLCollection 中的元素
- php - 在 PHP 中的特定部分之后从字符串中解析 int
- security - 尝试为 Content-Security-Policy 创建白名单