- Home /
Bomberman - bombAmountScript
I'm making a bomberman-like game. Right now I want to make some kind of a "bomb ammunition" - if a player has at least one bomb and press a certain key, he makes a bomb - the bomb explodes. Bomb was made - one less bomb in the ammuntion, bomb exploded - one more bomb in ammunition. Here's my code :
public class BombMaking : MonoBehaviour
{
public GameObject bomb;
private static int bombAmount = 2;
public void AddABomb()
{
bombAmount += 1;
}
public void SubstractABomb()
{
bombAmount -= 1;
}
void MakeABomb()
{
Instantiate(bomb, Vector3Int.RoundToInt(this.gameObject.transform.position), Quaternion.identity);
}
void Update()
{
if(Input.GetKeyDown("space") && bombAmount > 0)
{
MakeABomb();
SubstractABomb();
}
}
}
And the second part with bomb exploding :
public GameObject player;
private BombMaking bombMaking;
public float explosionDuration = 0.4f;
void Start()
{
bombMaking = player.GetComponent<BombMaking>();
}
public void Explode()
{
// creating an explosion in a place where the bomb is
GameObject explosion = Instantiate(explosionPrefab, this.gameObject.transform.position, Quaternion.identity) as GameObject;
//destroying it
Destroy(explosion, this.explosionDuration);
// adding a bomb
bombMaking.AddABomb();
}
Everything works fine when I set the amount to 1, but when it comes to 2 something is wrong.... After explosion of the first 2 bombs I can make even 4, and when these 4 explode I can make much more... Maybe it's something with the static type, I'm not sure if I use it in a correct way... BombMaking script is attached to the player, code with Explode method is attached to the bomb. Explode is called when the animation of the bomb ends.
PS: I didn't show the rest of the code, I think it's not necessery ;)
I can't see what might be causing it from what you've posted. $$anonymous$$y first guess would be that either Explode() or AddABomb() is being called multiple times. Try adding a breakpoint or some debug output on those functions to ensure they're not being hit more than once.
And I don't think bombAmount needs to be static either. Unless there's some reason I'm not seeing?
ya it doesn't look like it needs to be static, but that shouldn't matter, unless there are more than one instance of the Bomb$$anonymous$$aking class.
Yeah, I can't see where the 'Explode' is being called. In any case, it can be potentially called on the same bomb multiple times. Perhaps checking if the bomb hasn't exploded there yet might be a good safequard against it. Also, don't make the bombAmount static. Ins$$anonymous$$d, give the bomb instance a reference to the Bomb$$anonymous$$aking script that added it, or give the Bomb an event that's called when it explodes and hook the bomb$$anonymous$$aking. AddABomb to it, don't forget to unhook after.
Okay, so Right now it adds the bomb to my prefab ( it is selected as an GameObject in "public GameObject player", but it substracts the bomb from the player prefab instance in the game... After making e.x. 2 bombs the prefab has 4, and the player in the game has 0. What should I do? I changed public static bombAmount to just public bombAmount .
Explode method as I mentioned is called as an event after finishing the animation of the bomb . I can give some pictures if something is not clear.
Answer by NotNew · Feb 22, 2020 at 06:14 PM
Okay, so I solved this using the Invoke function and setting the time equal to the time that animation of the bomb lasts for, sth like this : public class BombMaking : MonoBehaviour { public GameObject bomb; public int bombAmount = 1;
public void AddABomb()
{
bombAmount += 1;
}
public void SubstractABomb()
{
bombAmount -= 1;
}
void MakeABomb()
{
Instantiate(bomb, Vector3Int.RoundToInt(this.gameObject.transform.position), Quaternion.identity);
}
void Update()
{
if(Input.GetKeyDown("space") && bombAmount > 0)
{
SubstractABomb();
MakeABomb();
Invoke("AddABomb", 2f);
}
}
}
And deleted the calls of AddABomb from Explode method. Is it okay to do it this way? I mean, I'm new to Unity so I don't know whether it's a piece of .... , or it's okay. Now everything works properly with it.
There's an argument to be made that if it works, then it's okay :P
Personally I'd want to know why it didn't work in the first place (I still suspect Explode() is being called twice; animation events can be tricksy...), but if you're happy with it then that should be good enough!
Your answer
![](https://koobas.hobune.stream/wayback/20220612223801im_/https://answers.unity.com/themes/thub/images/avi.jpg)