2017-03-11 4 views
-1

であるため、私は最近C#を学び始めましたが、私はあなたのうちの何人かが私を助けてくれることを願っています。私たちはUnity5でゲームを開発しています。選手の健康が0になると、人形の機能が蹴られますが、残念ながら私は方法グループなので死を呼び出すことはできません。乾杯。ここでは、コード(エラーがライン47と53で私をせていなかったので、私はので、私はユニティでプレイモードに入ることができ、それをコメントアウト)です:"Death"はメソッドグループが

using UnityEngine; 
using System.Collections; 

public class EnemyAI_Basic : MonoBehaviour 
{ 
    private EnemyAI_Basic enemyAI_Basic; 
    Animator controller; 
    float health; 
    Animator anim; 
    bool Alive = true; 
    public bool Dead = false; 
    int pointValue = 5; 
    private Collider myCollider; 
    private Rigidbody myRigidbody; 

    CapsuleCollider capsuleCollider; 


    void Start() 
    { 
     controller = GetComponentInParent<Animator>(); 
     health = 40; 
     capsuleCollider = GetComponent<CapsuleCollider>(); 
     anim = GetComponent<Animator>(); 
    } 

    void Update() 
    { 
     if (!Dead) 
     { 
      anim.SetTrigger("Alive"); 
     } 
    } 

    void Death() 
    { 
     Dead = true; 
     Alive = false; 
     capsuleCollider.isTrigger = true; 
     anim.SetTrigger("Dead"); 
     Destroy(gameObject, 4f); 
    } 

    void OnEnable() 
    { 
     SetInitialReferences(); 
     enemyAI_Basic.Death += ActivateRagdoll; 
    } 


    void OnDisable() 
    { 
     enemyAI_Basic.Death -= ActivateRagdoll; 
    } 

    void SetInitialReferences() 
    { 
     enemyAI_Basic = transform.root.GetComponent<EnemyAI_Basic>(); 

     if (GetComponent<Collider>() != null) 
     { 
      myCollider = GetComponent<Collider>(); 
     } 

     if (GetComponent<Rigidbody>() != null) 
     { 
      myRigidbody = GetComponent<Rigidbody>(); 
     } 

    } 

    void ActivateRagdoll() 
    { 
     if (myRigidbody != null) 
     { 
      myRigidbody.isKinematic = false; 
      myRigidbody.useGravity = true; 
     } 

     if (myCollider != null) 
     { 
      myCollider.isTrigger = false; 
      myCollider.enabled = true; 
     } 
    } 
} 
+1

なぜあなたはあなたの 'Death'メソッドの中で' ActivateRagdoll'を呼んでいませんか?私はあなたが 'enemyAI_Basic.Death + = ActivateRagdoll; 'を見つけたのか、なぜこれがあなたがしたいことをしたのか考えていませんでした。 – UnholySheep

+2

' Death'はメソッドであり、イベントではありません。多分、C#https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71)のイベントをお読みください。aspx – juharr

+0

さて、私はチュートリアルやそれをモデリングして、他の部分を追加して助けてくれてありがとうございます(私は本当にnoobです)。後でもう一度見て、チュートリアルを見る予定です。 – Napter77

答えて

0

私はあなたの問題を参照してください。 あなたはデリゲートのような死の方法を扱っていますが、この場合はそうではありません。デリゲートまたはイベントのいずれかにのみメソッドを登録できます。メソッドをメソッドグループに登録することはできません。 状況によっては、これを解決する方法がいくつかあります。

void Death() 
{ 
    Dead = true; 
    Alive = false; 
    capsuleCollider.isTrigger = true; 
    anim.SetTrigger("Dead"); 
    ActivateRagdoll(); 
    Destroy(gameObject, 4f); 
} 

その後削除:

enemyAI_Basic.Death += ActivateRagdoll; 

enemyAI_Basic.Death -= ActivateRagdoll; 
そうは次のように、死の関数内ActivateRagdollへの呼び出しに入れ

まずソリューション、およびはるかに最も容易な、 OnEnableメソッドおよびOnDisableメソッドからの

を使用すると、コンパイラエラーがなくなります。 今、このメソッドを使用するには、コードのどこかから呼び出すだけです。あなたが愛を殺す方法に応じて、あなたはその方法を公開することを検討したり、更新ループ内いくつかの死のロジックを有していてもよく、

if(health <= 0) 
{ 
    Death(); 
    enabled = false; //disable the component so the update loop doesn't repeat the death procedure multiple times, Unity might not be pleased. 
} 

他の方法、そしてあなたがそれをやろうとしている方法であり、クラスがメソッドをサブスクライブできるイベントを作成します。このイベントは、購読されているすべてのメソッドを呼び出し、通知される手段を提供します。これは、常にAiの死の状態をチェックする必要はありません外部のクラスに役立つことができる、結局のところ、彼らは完全な健康の上にAiに関心を持っていない可能性があります。彼らはアイが死んだときだけ知る必要があります。イベントはこのような状況に最適です。そここのようなシステムアップリグには、いくつかの他の方法がありますが、あなたの場合には、これはあなたが後にしているものかもしれ

public class EnemyAI_Basic : MonoBehaviour 
{ 
    //your other variables 

    public delegate void EnemyDies(); //declare the method types that can be registered to the event 

    public event EnemyDies onEnemyDeath; //declare event, using the delegate for the method type 

    //other methods here 

    private void OnEnable() 
    { 
     //must be the same method type as the event, the compiler will let you know if it isn't 
     onEnemyDeath += Death; 
     onEnemyDeath += ActivateRagdoll;    
    } 

    private void OnDisable() 
    { 
     onEnemyDeath -= Death; 
     onEnemyDeath -= ActivateRagdoll; 
    } 

    //other things here 

    //doesn't have to be public, just in case you want a bullet to kill the enemy 
    public void KillAi() 
    { 
     //checking for null basically says: is anybody registered? If not, nothing to invoke. 
     //It will get upset if you try and invoke an event without any registered methods, hence the check. 
     if(onEnemyDeath != null) 
     { 
      onEnemyDeath(); 
     } 
    } 

    void Death() 
    { 
     Dead = true; 
     Alive = false; 
     capsuleCollider.isTrigger = true; 
     anim.SetTrigger("Dead"); 
     //ActivateRagdoll(); You don't have to manually call this now, invoking the event will call it for you. 
     //Registration order is important, me thinks. Experiment :) 
     Destroy(gameObject, 4f); 
    } 
} 

:あなたはこのような何かをやっているだろうイベントを作るために

いくつかの読書:

イベント:https://www.codeproject.com/Articles/11541/The-Simplest-C-Events-Example-Imaginable https://www.tutorialspoint.com/csharp/csharp_events.htm

代表者:https://msdn.microsoft.com/en-us/library/ms173171.aspx

方法グループ:What is a method group in C#?

うまくいけば、このことができます。

+0

それは働いてくれてありがとう、ありがとうございました。 – Napter77