前提・実現したいこと
UnityでhitPointが0以下になった時に、指定のアニメーションに遷移させたい。
発生している問題・エラーメッセージ
Unityでアニメーションのスクリプト制御を行おうとしたところ、以下のエラーが発生し、
アニメーションが遷移しない。
エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object
EnemyController.Update () (at Assets/Script/EnemyController.cs:19)
該当のソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController: MonoBehaviour
{
private Animator animator;
public int hitPoint = 100;
void start() { animator = GetComponent<Animator>(); } void Update() { if (hitPoint <= 0) { this.animator.SetTrigger("Die"); } } public void Damage(int damage) { hitPoint -= damage; }
}
試したこと
以下の様に最小限の記述でテストすると、アニメーション遷移できますが、
ヒットポイント管理関係の記述を追加すると上記エラーが発生します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private Animator animator;
void Start() { animator = GetComponent<Animator>(); } void Update() { this.animator.SetTrigger("Die"); }
}
補足情報(FW/ツールのバージョンなど)
上記のコード以外に、以下のコードを別に記述して連携させています。
上記以外のエラーは発生していません。
Unity のバージョンは 2019.2.1f1 personal です。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public int damage; private GameObject enemy; private EnemyController hp; void Start() { enemy = GameObject.Find("Enemy"); hp = enemy.GetComponent<EnemyController>(); } void OnTriggerEnter(Collider other) { //ぶつかったオブジェクトのTagにShellという名前が書いてあったならば(条件). if (other.CompareTag("Shell")) { //EnemyControllerクラスのDamage関数を呼び出す hp.Damage(damage); //ぶつかってきたオブジェクトを破壊する. Destroy(other.gameObject,3.0f); } }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/24 16:26