在创建的类array中,由GameObject. 然后,当尝试创建变量blocksAmount时,它以红色突出显示并引发错误:
CS0236 : A field initializer cannot reference the nonstatic field, method, or property 'Ball.initialBlocksAmount'
下面是代码。
public class Ball : MonoBehaviour
{
[SerializeField] GameObject[] initialBlocksAmount = UnityEngine.GameObject.FindGameObjectsWithTag("block");
int blocksAmount = initialBlocksAmount.Length;
请帮助我理解错误。
数量只能在方法内部获得,例如
在编译之前,在游戏开始之前,在创建对象之前,编译器一无所知
initialBlocksAmount,也无法计算任何长度。因为它只是一个类,草稿,草图