Unity3D: NullReferenceException: Object reference not set to an instance of an object -


i want access hero.class variable "aspect" lasercontroller.class, receive error message : nullreferenceexception: object reference not set instance of object.

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

i guess hero component isn't attached same gameobject lasercontroller attached to. if want force condition can use requirecomponentattribute:

[requirecomponent(typeof(hero))] public class lasercontroller : monobehaviour  

some other unrelated consideration:

  • defining empty update method useless , has performances overhead
  • try follow consistence naming conventions classes (camel case: lasercontroller -> lasercontroller)

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -