0

I'm in search of problem solution for a game I'm making.

There is a player that needs to evade obstacles and I want to make my Score to stop when player collides with obstacle and also further I want to make a highscore count.

But for now I'm getting an error when the player collides with an obstacle.

the code for the player is

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

public class Player : MonoBehaviour

{
    public GameObject deathcanvas;
    public float moveSpeed = 600f;
    public Text scoreText;
    float movement = 0f;
    private bool isDead = false;
    // Update is called once per frame
    void Update()
    {           
        movement = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        if (isDead)
            return;
        transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Death();
    }
        private void Death()
    {
        isDead = true;
        GetComponent<Score> ().OnDeath(); 
        deathcanvas.SetActive(true);
    }
}

also there is a code for score counter that is

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


public class Score : MonoBehaviour
{
    public int Player_Score;
    public Text txt;
    private float clock;
    private bool isDead = false;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (isDead)
            return;
        txt.text = "Score: " + Player_Score;
        clock += 1 * Time.deltaTime;
        if (clock > 1)
        {
            Player_Score += 1;
            clock = 0;
        }
    }
    public void OnDeath()
    {
        isDead = true;
    }
}

and there is also a code for obstacle spawner, but i don't think that it is needed. And the error for when the player collides with an obstacle is

NullReferenceException: Object reference not set to an instance of an object
Player.Death () (at Assets/scripts/Player.cs:36)
Player.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/scripts/Player.cs:31)

What should i do?

derHugo
  • 83,094
  • 9
  • 75
  • 115
Gexila
  • 1
  • Sounds like either `GetComponent()` returned nothing since the component is not on the same object as the `Player` script or `deathcanvas` is not referenced ... – derHugo Nov 28 '20 at 19:21

1 Answers1

-1

There are some strange things here, why do you have 2 functions of death and 2 book of death? It's useful, since Score is on player like the other script you can put everything inside the Player script and access to the canvas and score text without using public functions and a lot of variables. If it's still an error then we can see something else

Leoverload
  • 1,194
  • 3
  • 8
  • 21