[英]C# error: An object reference is required for the non-static field, method, or property
I want my player to give a speed boost for a few seconds. 我希望我的播放器加快速度几秒钟。 When it collects 4 items (paintCount = 4), the player gets a movement speed boost for a short period of time. 当它收集4个项目(paintCount = 4)时,玩家会在短时间内获得移动速度提升。 I always have an error in the class Paintser: SimplePlayer0.SpeedUp();
我在Paintser类中总是出错: SimplePlayer0.SpeedUp();
. 。 I've tried many things to counter it, but none of them are working. 我已经尝试了很多方法来解决它,但是没有一个起作用。 I'm working in Unity. 我在Unity工作。
Error: An object reference is required for the non-static field, method, or property 'SimplePlayer0.SpeedUp(). 错误:非静态字段,方法或属性'SimplePlayer0.SpeedUp()需要对象引用。
This is the players script: 这是玩家脚本:
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;
}
}
This is the power up script, where the gameobject gets destroyed. 这是加电脚本,其中游戏对象被销毁。
using UnityEngine;
using System.Collections;
public class PowerUp : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Paintser.ExtraTime();
Destroy(this.gameObject);
Paintser.paintCount++;
}
}
}
And finally the script where all the magic (or errors) happens: 最后是所有魔术(或错误)发生的脚本:
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
method is an instance member of SimplePlayer0
class. SpeedUp
方法是SimplePlayer0
类的实例成员 。
Thus, you need to call it as an instance method: 因此,您需要将其作为实例方法调用:
SimplePlayer0 player0 = new SimplePlayer0();
player0.SpeedUp();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.