2
我想從laserController.class訪問Hero.class變量「方面」的一個實例,但我收到錯誤消息:NullReferenceException: Object reference not set to an instance of an object
。Unity3D:的NullReferenceException:對象沒有設置爲一個對象
Hero.class
using UnityEngine;
using System.Collections;
public class Hero : MonoBehaviour {
public float aspect = 0.1f;
void Update() {
}
}
laserController.class
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);
}
}
可能的重複[W帽子是一個NullReferenceException,如何解決?(http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – LearnCocos2D