Unity 5非静态字段,方法或属性错误需要对象引用 [英] Unity 5 An object reference is required for the non-static field, method, or property error

查看:798
本文介绍了Unity 5非静态字段,方法或属性错误需要对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unity 5中,在PlayerMovement类的第35行上使用c#出现此错误:

In Unity 5 using c# on line 35 of my PlayerMovement class I have this error:

错误CS0120非静态字段需要对象引用, 方法或属性"GameManager.completeLevel()"

Error CS0120 An object reference is required for the non-static field, method, or property 'GameManager.completeLevel()'

PlayerMovement类:

PlayerMovement Class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {
    public GameObject deathParticals;
    public float moveSpeed;
    private Vector3 spawn;

    // Use this for initialization
    void Start () {
        spawn = transform.position;
        moveSpeed = 5f;
    }

    // Update is called once per frame
    void Update() {
        transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
        if (transform.position.y < -2)
        {
            Die();
        }
    }
    void OnCollisionStay(Collision other)
    {
        if (other.transform.tag == "Enemy")
        {
            Die();
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.transform.tag == "Goal")
        {
            GameManager.completeLevel();
        }
    }
    void Die()
    {
        Instantiate(deathParticals, transform.position, Quaternion.identity);
        transform.position = spawn;
    }
}

GameManager类:

GameManager Class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
    public static int currentScore;
    public static int highscore;

    public static int currentLevel = 0;
    public static int unlockedLevel; 

    public void completeLevel()
    {
        currentLevel += 1;
        Application.LoadLevel(currentLevel);
    }
}

推荐答案

当您的GameManager类继承自MonoBehaviour时,此脚本需要附加到游戏对象上.然后,您需要获取对GameManager组件的引用,可以通过几种方式来完成.

As your GameManager class inherits from MonoBehaviour, this script needs to be attached to a game object. Then you need to get a reference to the the GameManager component, which can be done in several ways.

GameManager gameMananger = GameObject.Find("GameManager").GetComponent<GameManager>();

如果您的游戏对象称为"GameManager",则上述方法将起作用,如果将其称为其他名称,则将其替换为您的对象的名称.

The above will work if your game object is called "GameManager", swap this for the name of your object if it is called something else.

然后gameMananger.completeLevel();现在可以工作.

您可以将其缩短为 GameObject.Find("GameManager").GetComponent<GameManager>().completeLevel();

如果GameManager脚本与PlayerMovement附加到相同的gameObject上,这就是您所需要的GetComponent<GameManager>().completeLevel();

If the GameManager script is attached to the same gameObject as PlayerMovement, this is all you need GetComponent<GameManager>().completeLevel();

这篇关于Unity 5非静态字段,方法或属性错误需要对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆