2016-12-23 6 views
-1

のために必要である私は、このエラーを持っている:ユニティ5オブジェクト参照が私のPlayerMovementクラスのライン35上のC#を使用してユニティ5では非静的フィールド、メソッド、またはプロパティのエラー

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

PlayerMovementクラス:

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クラス:

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); 
    } 
} 
+0

VAR GM =新しいGameManagerである(を必要とするすべてである GameObject.Find("GameManager").GetComponent<GameManager>().completeLevel();

にこれを短縮することができ、 ? –

答えて

0

あなたはGameManagerオブジェクトをインスタンス化する必要があります。

public GameObject gameManager; 
Instantiate(gameManager) 

それともジムが述べたように:

you need to mark completeLevel as static:

static public void completeLevel() 
{ 
    .... 
} 

はもっとここで見つける: Unity Tutorial: gameManager

1

をごGameManagerクラスがMonoBehaviourから継承したように、このスクリプトはゲームオブジェクトに添付する必要がありますが。次に、GameManagerコンポーネントへの参照を取得する必要があります。これはいくつかの方法で実行できます。

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

は、上記のあなたのゲームのオブジェクトは、「GameManager」と呼ばれているならば、それは何かを呼ばれた場合、あなたのオブジェクトの名前のためにこれを交換していきます。

次にgameMananger.completeLevel();が機能するようになりました。

あなたはGameManagerスクリプトがPlayerMovementと同じゲームオブジェクトに接続されている場合、これは)あなたがGetComponent<GameManager>().completeLevel();

関連する問題