c# - Unity3D Passing Value Script to Other Script -


i have 2 scripts. first script ballcontrol attached on gameobject. second script hero attached on other gameobject. when try passing value hero ballcontrol, receive error message : "nullreferenceexception: object reference not set instance of object" how can solve problem or how can pass value script attached on object other script attached on other object? time.

using unityengine; using system.collections;  public class ballcontrol : monobehaviour {      public int life = 0;     public gameobject hero;      void update () {          hero obj = getcomponent<hero>();         life = obj.lifeball;          if(life==20){             print("gameover");         }        } } 

//

using unityengine; using system.collections;  public class hero : monobehaviour {      public int lifeball = 0;     public gameobject ball;      void update () {         lifeball++;     }  } 

as said hero attached game object, need reference other object. assuming gameobject hero 1 containing hero component, need:

        hero obj = hero.getcomponent<hero>(); 

assure have dragged game object of hero member hero of ball controlling game object.

anyway life easier if declare public hero hero instead of public gameobject hero , drag game object of hero it. don't need call getcomponent can use directly.


Comments