hi, I am having problems with my code can’t seem to get the score and points to work. there are no errors showing with my code, the score just won’t change on UI when I kill the enemies and there is no point given to the Player. so here I have posted all my code
Player Code
[SerializeField]
private float _speed = 3.5f;
private float _speedMultiplier = 2;
[SerializeField]
private GameObject _LazerPrefab;
[SerializeField]
private GameObject _TripleShotPrefab;
[SerializeField]
private float _fireRate = 0.5f;
private float _canFire = 1f;
[SerializeField]
private int _lives = 3;
private SpawnManager _spawnManager;
private UIManager _uiManager;
[SerializeField]
private bool _isTripleShotActive = false;
[SerializeField]
private bool _isSpeedBoostActive = false;
private bool _isShieldsActive = false;
[SerializeField]
private GameObject _shieldVisualizer;
[SerializeField]
private int _score;
// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(0, 0, 0);
_spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
if (_spawnManager == null)
{
Debug.LogError("the spawn manager is null");
}
if (_uiManager == null)
{
Debug.LogError("the UI Manager is NULL.");
}
}
// Update is called once per frame
void Update()
{
CalculateMovement();
if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire)
{
FireLazer();
}
}
void CalculateMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float VerticalInput = Input.GetAxis("Vertical");
Vector3 direction = new Vector3(horizontalInput, VerticalInput, 0);
transform.Translate(direction * _speed * Time.deltaTime);
transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0);
if (transform.position.x > 11.3f)
{
transform.position = new Vector3(-11.3f, transform.position.y, 0);
}
else if (transform.position.x < -11.3f)
{
transform.position = new Vector3(11.3f, transform.position.y, 0);
}
}
void FireLazer()
{
_canFire = Time.time + _fireRate;
if (_isTripleShotActive == true)
{
Instantiate(_TripleShotPrefab, transform.position, Quaternion.identity);
}
else
Instantiate(_LazerPrefab, transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
}
public void Damage()
{
if (_isShieldsActive == true)
{
_isShieldsActive = false;
_shieldVisualizer.SetActive(false);
return;
}
_lives -= 1;
if(_lives < 1)
{
_spawnManager.OnPlayerDeath();
Destroy(this.gameObject);
}
}
public void TripleShotActive()
{
_isTripleShotActive = true;
StartCoroutine(TripleShotPowerDownRoutine());
}
IEnumerator TripleShotPowerDownRoutine()
{
yield return new WaitForSeconds(5.0f);
_isTripleShotActive = false;
}
public void SpeedBoostActive()
{
_isSpeedBoostActive = true;
_speed *= _speedMultiplier;
StartCoroutine(SpeedBoostPowerDownRoutine());
}
IEnumerator SpeedBoostPowerDownRoutine()
{
yield return new WaitForSeconds(5.0f);
_isSpeedBoostActive = false;
_speed /=_speedMultiplier;
}
public void ShieldActive()
{
_isShieldsActive = true;
_shieldVisualizer.SetActive(true);
}
public void AddScore(int points)
{
_score += points;
_uiManager.UpdateScore(_score);
}
}
Enemy Code
public class Enemy : MonoBehaviour
{
[SerializeField]
private float _speed = 4.0f;
private Player _player;
// Start is called before the first frame update
void Start()
{
_player = GameObject.Find("Player").GetComponent<Player>();
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
if (transform.position.y < -5f)
{
float randomX = Random.Range(-8, 8f);
transform.position = new Vector3(randomX, 7, 0);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Player player = other.transform.GetComponent<Player>();
if (player != null)
{
player.Damage();
}
Destroy(this.gameObject);
}
if (other.tag == "Lazer")
{
Destroy(other.gameObject);
if (_player != null)
{
_player.AddScore(10);
}
Destroy(this.gameObject);
}
}
}
UI Manager Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
[SerializeField]
private Text _scoreText;
// Start is called before the first frame update
void Start()
{
_scoreText.text = "Score: " + 0;
}
public void UpdateScore(int playerScore)
{
_scoreText.text = "Score: " + playerScore;
}
}
here is all the code that has the scoring system any help would great thank you