0

I am trying to get my game so when you die and the death screen pops up, the obstacles stop moving. This is the error message that pops up:

NullReferenceException: Object reference not set to an instance of an object FlipGravity.Update () (at Assets/Scripts/FlipGravity.cs:56)

What is the error in my code?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class FlipGravity : MonoBehaviour {

    private Rigidbody2D rb;
    public int health = 3;
    
    public Text ScoreText;
    public int NumOfHearts;
    public Image[] hearts;
    public Sprite FullHeart;
    public Sprite EmptyHeart;
    public GameObject GameOver;
    void Start(){
    
        rb = GetComponent<Rigidbody2D>();
    
    

    

    }
    void Update(){
        
        for (int i = 0; i < hearts.Length; i++)
        {
            if(i<health)
            {
                hearts[i].sprite = FullHeart;
            } else
            {
                hearts[i].sprite = EmptyHeart;
            }
            if(i < NumOfHearts){
                hearts[i].enabled = true;
            }else {
                hearts[i].enabled = false;
            }
        }
        if(Input.GetKeyDown(KeyCode.Space)){
             rb.gravityScale *= -1;
             
    
        }
        if(health <= 0){
           
            GameOver.SetActive(true);
            Destroy(gameObject);
            //GetComponent<Obstacle>().speed = 0;
            GameObject Spikes = GameObject.Find("Spikes");
            Obstacle playerScript = Spikes.GetComponent<Obstacle>();
            playerScript.speed = 10f;
        }
        if(health > 0){
            GameOver.SetActive(false);

        }

    }
    /*void OnTriggerEnter2D(Collider2D other){
        if(health <= 0){
            other.GetComponent<Obstacle>().speed = 0;
        }
    }*/
    
        
        
    
    
}

Just to clarify, it lets me start and the error message pops up after I die

0 Answers0