hi can someone please help me with error, I have tried youtube and google but i just don’t understand what I need to do. also, I need this done soon as I am making the game as a gift for a friend.
NullReferenceException: Object reference not set to an instance of an object
Player.Damage () (at Assets/Scripts/Player.cs:109)
Enemy.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/Enemy.cs:38)
and here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[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;
[SerializeField]
private bool _isTripleShotActive = false;
[SerializeField]
private bool _isSpeedBoostActive = false;
private bool _isShieldsActive = false;
[SerializeField]
private GameObject _shieldVisualizer;
private UIManager _uiManager;
// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(0, 0, 0);
_spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
if (_spawnManager == 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 --;
_uiManager.UpdateLives(_lives);
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);
}
}
but if you can, can you explain clearly because I have learn disabilities so I find it hard to understand