Adding a scoreboard using a public class to call in another script.
This is the script called ScoreBoard:
public class ScoreBoard : MonoBehaviour
{
int score;
public void IncreaseScore(int amountToIncrease)
{
score += amountToIncrease;
Debug.Log($"Score is now: {score}");
}
}
Then I am calling it in my Enemy Script:
public class Enemy : MonoBehaviour
{
[SerializeField] GameObject deathVfx;
[SerializeField] Transform parent;
[SerializeField] int scorePerHit = 15;
ScoreBoard scoreBoard;
void start()
{
scoreBoard = FindObjectOfType<ScoreBoard>();
}
void OnParticleCollision(GameObject other)
{
scoreBoard.IncreaseScore(scorePerHit);
GameObject vfx = Instantiate(deathVfx, transform.position, Quaternion.identity);
vfx.transform.parent = parent;
Destroy(gameObject);
}
}
The issue is as soon as I add the line scoreBoard.IncreaseScore(scorePerHit); to enemy.cs , the rest of the collision stops working and I get the error “Object reference not set to an instance of an object”