Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NotNew · Feb 22, 2020 at 12:24 PM · function callstatic variablebomb

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 ;)

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alwayscodeangry · Feb 22, 2020 at 01:24 PM 1
Share

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.

avatar image alwayscodeangry · Feb 22, 2020 at 01:26 PM 0
Share

And I don't think bombAmount needs to be static either. Unless there's some reason I'm not seeing?

avatar image logicandchaos alwayscodeangry · Feb 22, 2020 at 02:27 PM 0
Share

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.

avatar image Pangamini · Feb 22, 2020 at 02:37 PM 0
Share

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.

avatar image NotNew · Feb 22, 2020 at 06:00 PM 0
Share

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 .

avatar image NotNew · Feb 22, 2020 at 06:04 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alwayscodeangry · Feb 23, 2020 at 01:08 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to draw a laneway/tunnel dynamically and simulate bombed hole 2 Answers

Ninja fruit - code correct but bomb missing 1 Answer

DontDestroyOnLoad Question 1 Answer

Acessing static variable from Entities.ForEach 1 Answer

How store a function with var to be called later? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges