[英]Error in C#: “An object reference is required for the non-static field, method, or property”
[英]C# error: An object reference is required for the non-static field, method, or property
我希望我的播放器加快速度几秒钟。 当它收集4个项目(paintCount = 4)时,玩家会在短时间内获得移动速度提升。 我在Paintser类中总是出错: SimplePlayer0.SpeedUp();
。 我已经尝试了很多方法来解决它,但是没有一个起作用。 我在Unity工作。
错误:非静态字段,方法或属性'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;
}
}
这是加电脚本,其中游戏对象被销毁。
using UnityEngine;
using System.Collections;
public class PowerUp : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Paintser.ExtraTime();
Destroy(this.gameObject);
Paintser.paintCount++;
}
}
}
最后是所有魔术(或错误)发生的脚本:
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;
}
}
}
SpeedUp
方法是SimplePlayer0
类的实例成员 。
因此,您需要将其作为实例方法调用:
SimplePlayer0 player0 = new SimplePlayer0();
player0.SpeedUp();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.