NullReferenceException: Object reference not set to an instance of an object
hye hi there i completed Roll a Ball Tutorial a month ago then other tutorials i learned alot then i leared Brick Ball. i just tride to add Score We Ball Hits Bricks But Geting Error ("NullReferenceException: Object reference not set to an instance of an object"), then i made Roll a Ball Game Then make a GameManager which can handle ScoreText System Same Like As My Brick Game but i got Same Error ("NullReferenceException: Object reference not set to an instance of an object"). Dont Know Y it Is happening when i me updating Score WithOut GameManager's Sript It Works Fine But When i use GameManager it gives error. i learned many things from unity n this is my First Question On Offical Site Hope So U Guys Help Me!!!!!!!!!!!!
There Are That Two Scripts : 1) using UnityEngine; using System.Collections;
public class Ball : MonoBehaviour {
Rigidbody rb;
// Use this for initialization void Start () { rb = GetComponent ();
}
// Update is called once per frame void Update () { float hori = Input.GetAxis("Horizontal"); float verti = Input.GetAxis("Vertical"); Vector3 move = new Vector3 (hori, 0.0f, verti); rb.AddForce (move * 10f); } void OnTriggerEnter(Collider other){ if (other.gameObject.CompareTag ("PickUps")) { Destroy (other.gameObject); GameControllor obj = GetComponent(); obj.SetScoreText(); } } }
2) using UnityEngine; using System.Collections; using UnityEngine.UI;
public class GameControllor : MonoBehaviour { public Text ScoreText; int Count;
// Use this for initialization void Start () { Count = 0; SetScoreText (); }
public void SetScoreText() { Count++; ScoreText.text = ("Score : " + Count); } }
Answer by Bob-The-Zealot · Aug 31, 2015 at 11:37 AM
In the editor, drag the needed object into the script component.
The problems not about the script.