Hello,
After adding the below line of code in enemy script, enemy is not dying any more. And score also not adding up
FindObjectOfType<GameSession>().AddToScore(scoreValue);
And giving below errors as well.
NullReferenceException: Object reference not set to an instance of an object
Enemy.Die () (at Assets/Scripts/Enemy.cs:75)
Enemy.ProcessHit (DamageDealer damageDealer) (at Assets/Scripts/Enemy.cs:69)
Enemy.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/Enemy.cs:60)
I have followed exactly as shown in the lecture, no changes. Only I am using Unity 2019.3.7f1 version.
Below are the scripts for reference.
- ScoreDisplay.cs
using UnityEngine;
using UnityEngine.UI;
public class ScoreDisplay : MonoBehaviour
{
Text scoreText;
GameSession gameSession;
void Start()
{
scoreText = GetComponent<Text>();
gameSession = FindObjectOfType<GameSession>();
}
void Update()
{
scoreText.text = gameSession.GetScore().ToString();
}
}
- GameSession.cs
using UnityEngine;
public class GameSession : MonoBehaviour
{
int score = 0;
int health = 0;
private void Awake()
{
SetUpSingleton();
}
private void SetUpSingleton()
{
int numberGameSessions = FindObjectsOfType<GameObject>().Length;
if (numberGameSessions > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
public int GetScore()
{
return score;
}
public void AddToScore(int scoreValue)
{
score += scoreValue;
}
public void ResetGame()
{
Destroy(gameObject);
}
}
- Enemy.cs
using UnityEngine;
public class Enemy : MonoBehaviour
{
[Header(“Enemy Stats”)]
[SerializeField] float health = 100;
[SerializeField] int scoreValue = 150;
[Header("Shooting")]
[SerializeField] float shotCounter;
[SerializeField] float minTimeBetweenShots = 0.3f;
[SerializeField] float maxTimeBetweenShots = 3f;
[SerializeField] GameObject projectile;
[SerializeField] float projectileSpeed = 10f;
[Header("Sound Effect")]
[SerializeField] GameObject deathVFX;
[SerializeField] float durationOfExplosion = 1f;
[SerializeField] AudioClip deathSound;
[SerializeField] [Range(0, 1)] float deathSoundVolume = 0.7f;
[SerializeField] AudioClip shootSound;
[SerializeField] [Range(0, 1)] float shootSoundVolume = 0.25f;
void Start()
{
shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
void Update()
{
CountDownAndShoot();
}
private void CountDownAndShoot()
{
shotCounter -= Time.deltaTime;
if (shotCounter <= 0f)
{
Fire();
shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
}
private void Fire()
{
GameObject laser = Instantiate(
projectile,
transform.position,
Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
AudioSource.PlayClipAtPoint(shootSound, Camera.main.transform.position, shootSoundVolume);
}
private void OnTriggerEnter2D(Collider2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
if (!damageDealer) { return; }
ProcessHit(damageDealer);
}
private void ProcessHit(DamageDealer damageDealer)
{
health -= damageDealer.GetDamage();
damageDealer.Hit();
if (health <= 0)
{
Die();
}
}
private void Die()
{
FindObjectOfType<GameSession>().AddToScore(scoreValue);
Destroy(gameObject);
GameObject explosion = Instantiate(deathVFX, transform.position, transform.rotation);
Destroy(explosion, durationOfExplosion);
AudioSource.PlayClipAtPoint(deathSound, Camera.main.transform.position, deathSoundVolume);
}
}