[英]How do I Fix This Reference Bug? Unity C# - CS0176
我正在尝试制作一个游戏,其中物品掉下来,你必须用盾牌挡住它们。 但是,我需要在不同的脚本中引用(下落物体的)速度。 我收到此错误: Destruction.cs(14,21): error CS0176: Member 'Losing.min' cannot be accessed with an instance reference; qualify it with a type name instead
Destruction.cs(14,21): error CS0176: Member 'Losing.min' cannot be accessed with an instance reference; qualify it with a type name instead
这是两个脚本(仅包括重要部分),对于奇怪的样式感到抱歉:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Destruction : MonoBehaviour
{
public Transform[] Transforms = new Transform[5];
public Losing losing;
public float min;
private void Start()
{
float min = losing.min; // I get the error here!
}
和我引用的脚本
using UnityEngine;
using System.Collections;
public class Losing : MonoBehaviour
{
public GameObject[] Addictions = new GameObject[5];
public Transform[] Transforms = new Transform[5];
public float fallSpd;
public static float min = 0.1f; // I want to reference this variable
这是一个static
值,您可以使用它的类型名称,例如Losing.min
。
Static 值使用类型名称而不是该类型的变量访问。
min
是 static 属性,因此您应该使用 class 名称而不是实例名称来访问它。
所以:
float min = losing.min; // I get the error here!
应该:
float min = Losing.min; // capital L, we are referencing the class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.