I have two scripts. The first script BallControl is attached on a GameObject. The Second script Hero is attached on other GameObject. When I try to passing value Hero to BallControl, I receive an error message : "NullReferenceException: Object reference not set to an instance of an object" How can I solve this problem or how can I pass value script attached on an object to other script attached on an other object? Thanks for your 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++;
}
}
Hero obj = GetComponent<Hero>();as this tries to get a script and not another separate game object.