C#错误:非静态字段、方法或属性需要对象引用
我想让我的球员提高几秒钟的速度。当它收集到4件物品(paintCount=4)时,玩家会在短时间内获得移动速度提升。 我总是在类Paintser中出错:C#错误:非静态字段、方法或属性需要对象引用,c#,unity3d,C#,Unity3d,我想让我的球员提高几秒钟的速度。当它收集到4件物品(paintCount=4)时,玩家会在短时间内获得移动速度提升。 我总是在类Paintser中出错:SimplePlayer0.SpeedUp()。 我已经尝试了很多方法来对抗它,但是没有一种有效。 我在团结一致地工作 错误:非静态字段、方法或属性“SimplePlayer0.SpeedUp()”需要对象引用 这是玩家脚本: using UnityEngine; using System.Collections; public class S
SimplePlayer0.SpeedUp()代码>。
我已经尝试了很多方法来对抗它,但是没有一种有效。
我在团结一致地工作
错误:非静态字段、方法或属性“SimplePlayer0.SpeedUp()”需要对象引用
这是玩家脚本:
using UnityEngine;
using System.Collections;
public class SimplePlayer0 : MonoBehaviour
{
// SPEEDVARIABLES
public static float speed = 3.5f;
// BONUSSPEED
private static float speedBoostTime;
public static float SpeedBoostTime
{
get
{
return speedBoostTime;
}
set
{
speedBoostTime = value;
}
}
// BONUSSPEED
public void SpeedUp()
{
speed *= 2;
SpeedBoostTime = 3; // seconds
}
void Update()
{
// BONUSSPEED
while (speedBoostTime > 0)
{
speedBoostTime -= Time.deltaTime;
if (speedBoostTime <= 0) speed /= 2;
}
}
最后是发生所有魔法(或错误)的脚本:
SpeedUp
方法是SimplePlayer0
类的实例成员
因此,您需要将其作为实例方法调用:
SimplePlayer0 player0 = new SimplePlayer0();
player0.SpeedUp();
错误消失了,谢谢。不幸的是,我的球员并没有突然跑得更快。@ToondeJonge我明白了。。。事实上,我不是Unity开发者,但我发现我可以在这个通用问题上为您提供帮助:DIt是我必须面对的一个难题。我会在最后找到答案^^@ToondeJong响应您的速度提升未应用,请验证您的代码是否在“SpeedUp();”方法中正在正确调用。看起来是这样,但我看不到的是,这种速度提升是如何以任何方式应用于角色移动的。你需要将这个应用到他的角色控制器或以某种方式转换。我没有上传玩家动作的部分。它位于SimplePlayer0类tho中。我只需要看看球员的速度,对吗?奖励速度在开始时起作用,但当我想用时间限制来控制它时,它就不起作用了。
using UnityEngine;
using System.Collections;
public class Paintser : PowerUp
{
public static int paintCount = 0;
public int speedBoostTime = 3;
public static void ExtraTime()
{
if (paintCount == 4)
{
SimplePlayer0.SpeedUp();
Paintser.paintCount = Paintser.paintCount = 0;
}
}
}
SimplePlayer0 player0 = new SimplePlayer0();
player0.SpeedUp();