I’m trying to create a clone of the Bomberman game. The player will have the ability to drop bombs, but the amount will be limited according to skill level. To implement the logic, my idea was as follows: When the player presses J, he instantiates a bomb. At this time, it is checked if the player has already exceeded the limit amount of his skill. If it doesn’t go over the limit, the bombsDropped variable is incremented.
The problem is to decrement this variable. I tried to create an event that is launched at the moment the bomb explodes, but since the bomb is instantiated, the reference is always null.
Thanks to everyone who tries to help!
Examples of problems:
Repository in GitHub:
https://github.com/MuriloCSeidenstucker/GIT_BombermanClone
Codes:
PlayerController.cs:
public class PlayerController : MonoBehaviour
{
[SerializeField] GameObject bombPrefab;
...
PlayerSkills playerSkills;
PlayerInput playerInput;
Bomb bombScript;
int bombsDropped;
void Start()
{
...
playerSkills = GetComponent<PlayerSkills>();
playerInput = GetComponent<PlayerInput>();
bombScript = FindObjectOfType(typeof(Bomb)) as Bomb;
if (bombScript != null)
{
bombScript.OnExplode += BombExploded;
}
}
private void OnDestroy()
{
if (bombScript != null)
{
bombScript.OnExplode -= BombExploded;
}
}
void Update()
{
if (playerInput.GetActionInput())
{
if (!IsSkillLimitExceeded())
{
DropBomb();
}
}
}
void FixedUpdate()...
void MovePlayer()...
void DropBomb()
{
Instantiate(bombPrefab, transform.position, transform.rotation);
bombsDropped++;
}
void BombExploded()
{
bombsDropped--;
}
bool IsSkillLimitExceeded()
{
if (bombsDropped == playerSkills.AmountBombs)
return true;
return false;
}
}
Bomb.cs:
public class Bomb : MonoBehaviour
{
public event Action OnExplode;
float timeToExplode = 2.0f;
void Start()
{
StartCoroutine(BombSettings());
}
IEnumerator BombSettings()
{
yield return new WaitForSeconds(timeToExplode);
if (OnExplode != null)
{
OnExplode.Invoke();
}
Destroy(gameObject);
}
}