Quantcast
Viewing latest article 25
Browse Latest Browse All 78

why do i get the error Object reference not set to an instance of an object?

when i click play i get this error NullReferenceException: Object reference not set to an instance of an object GravityScript.Update () (at Assets/Scripts/GravityScript.cs:22) i am trying to get the score from FoodCollider.cs and use it in GravityScript.cs FoodCollider.cs public float SmallIncrease; public float MediumIncrease; public float LargeIncrease; public float CameraZoom = 0.02f; public int Cooldown = 10; private float timeStamp; public int Mass = 1; public int score = 0; public string scoreText = "Score: 0"; public string SpaceDust = "Space Dust"; public string Asteroid = "Asteroid"; public string Planet = "Planet"; public string BlackHole = "BlackHole"; //void Start() //{ //MovementScript ms = GameObject.FindObjectOfType(); //} // void Update() // { // if (timeStamp <= Time.time) // { // timeStamp = Time.time + Cooldown; // transform.localScale -= new Vector3(Increase/ms.Speed, Increase/ms.Speed, Increase/ms.Speed); // } //} void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "SmallFood") { //if (other.transform.localScale =< transform.localScale) // { //} transform.localScale += new Vector3(SmallIncrease, SmallIncrease, CameraZoom); Destroy(other.gameObject); score += 10; scoreText = "Score : " + score; } if (other.gameObject.tag == "MediumFood") { transform.localScale += new Vector3(MediumIncrease, MediumIncrease, CameraZoom); Destroy(other.gameObject); score += 25; scoreText = "Score : " + score; } if (other.gameObject.tag == "LargeFood") { transform.localScale += new Vector3(LargeIncrease, LargeIncrease, CameraZoom); Destroy(other.gameObject); score += 60; scoreText = "Score : " + score; } // if (score >= 100) // { // GUI.Box(new Rect(20, 10, 100, 20), SpaceDust); // } // if (score >= 300) // { // GUI.Box(new Rect(20, 10, 100, 20), Asteroid); // } // if (score >= 2500) // { // GUI.Box(new Rect(20, 10, 100, 20), Planet); // } // if (score <= 2500) // { // GUI.Box(new Rect(20, 10, 100, 20), BlackHole); // } } void OnGUI() { GUI.Box(new Rect(10, 10, 100, 20), scoreText); } } GravityScript.cs private float maxGravDist = 5.0f; private GameObject[] planets; private float maxGravity = 10.0f; private Rigidbody rb; FoodColider scoreboard; public int score; void Start() { rb = GetComponent(); planets = GameObject.FindGameObjectsWithTag("Planet"); scoreboard = GetComponent(); } void Update() { score = scoreboard.score; maxGravDist = 3 * (score / 1000); } void FixedUpdate() { foreach (GameObject planet in planets) { float dist = Vector3.Distance(planet.transform.position, transform.position); if (dist <= maxGravDist) { Vector3 v = planet.transform.position - transform.position; rb.AddForce(v.normalized * (1.0f - dist / maxGravDist) * maxGravity); } } } } Any help would be appreciated :)

Viewing latest article 25
Browse Latest Browse All 78

Trending Articles