我想从laserController.class 访问Hero.class 变量“aspect”,但我收到一条错误消息:NullReferenceException: Object reference not set to an instance of an object
。
英雄类
using UnityEngine;
using System.Collections;
public class Hero : MonoBehaviour {
public float aspect = 0.1f;
void Update () {
}
}
激光控制器类
using UnityEngine;
using System.Collections;
public class laserController : MonoBehaviour {
public float health = 0f;
//public float aspect = 0.1f;
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.tag == "enemy"){
Destroy(gameObject);
Destroy(collision.gameObject);
}
}
void Update () {
Hero direction = gameObject.GetComponent<Hero>();
//LaserHealth
health += Time.deltaTime;
if(health > 7f){
Destroy(gameObject);
}
//problem in here
transform.Translate(Vector3.up * -direction.aspect);
}
}