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?