Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Add energy (ammo) on collision

Discussion in 'Scripting' started by KrisBendix, Jun 11, 2015.

  1. KrisBendix

    KrisBendix

    Joined:
    May 26, 2015
    Posts:
    15
    Hi.
    How do I add 2 energy to private int energy = 10; (with energy --; each shot takes 1 away) each time I hit the target.
    I guess it should work with void OnCollisionEnter, but what is the script for adding?
    Whatever I was able to pull out of google (++, -+, =+...) didn't work. I feel like I'm close, but...

    I don't need anything complicated. Simply if hits, then adds, if doesn't hit, then just take 1 away as it does.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    The general form to assign a variable is:
    Code (csharp):
    1. variableName = newValue;
    (where newValue can include all sorts of math and functions and such)
    So, naturally, you can make newValue be a math formula based on the previous value of the variable:
    Code (csharp):
    1. variableName = variableName + 2;
    As a shortcut, C# provides this operator as well:
    Code (csharp):
    1. variableName += 2;
     
  3. KrisBendix

    KrisBendix

    Joined:
    May 26, 2015
    Posts:
    15
    OK, It doesn't like the
    Code (CSharp):
    1. void OnCollisionEnter (Collision col)
    2.     {
    3.         if(col.gameObject.name == "bubble(Clone)")
    4.         {
    5.             energy += 2;
    6.         }
    7.     }
    for some reason.
    When I put energy += 2; after if (Input.GetMouseButtonDown(0)){ it works. Of course it adds energy every time I press mouse.
    So why it doesn't work in void OnCollisionEnter? bubble(clone) is the target.
     
    Last edited: Jun 11, 2015
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Put in two Debug.Log statements, one inside and one outside the "if" block, and see which (if either) gets output to the console. That'll let you see if it's the collision part that's the problem, or the if statement.
     
  5. KrisBendix

    KrisBendix

    Joined:
    May 26, 2015
    Posts:
    15
    Hm... yes, I don't get output from either.
    May be it is because the void OnCollisionEnter for bubble is in wrong script. I will look in to this and will see if that's the problem.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    The script must be on the same GameObject as the collider, and both colliders involved must not be triggers. Also, you may need to check your layer collisions (Physics settings - grid of a bunch of checkbox toggles, to be safe make sure all are checked) to make sure the two objects can collide with each other.
     
  7. KrisBendix

    KrisBendix

    Joined:
    May 26, 2015
    Posts:
    15
    OK! Success!
    Now I did it from actual bubble object script and sent the 'energy += 2' to ProjectileShooter script by using GameObject projectileShooterObject = GameObject.FindWithTag...
    Before I was trying to do it in that projectile shooter script, since it is there where energy is reduced when projectile is launched.
    I actually tried this before, but then I forgot to Tag the Projectile Shooter. Gave up when it didn't work. Have to be more careful with little details. ^_^

    Thanks!