Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 VGeorgeV · May 04, 2012 at 10:59 PM · gunpickupsendmessageammo

Pick Up Script "overflowing"

Hey guys I'm having and issue with my pick scripts. I have sit a "ammo limit" and set my pick up item to send a message to add an amount of ammo to the gun. Lets say my gun shoots 199 out of 200 ammo, then when I pick up my ammo it adds more then the 200 max. Like if I was at 199 it makes it 209 and then adds no more if I pick up more ammo. Here are my scripts.

the pick up box "pick up" #pragma strict

var ammoPickUp = 10;

function OnCollisionEnter (col : Collision) { col.gameObject.BroadcastMessage("ammoPickedUp", ammoPickUp, SendMessageOptions.DontRequireReceiver); Destroy(gameObject); }

An this is the Gun it self script

pragma strict

// shoot obj machineGun

var projectile : Transform; var bulletSpeed : float = 20; var damage = 10; static var ammoMG = 200;

function Update () { // Put this in your update function if (Input.GetButtonDown("MGfire")&& ammoMG > 0) {

 // Instantiate the projectile at the position and rotation of this transform
 var clone : Transform;
 clone = Instantiate(projectile, transform.position, transform.rotation);

 // Add force to the cloned object in the object's forward direction
 clone.rigidbody.AddForce(clone.transform.forward * bulletSpeed);
 }
 if (Input.GetButtonDown("MGfire")&& ammoMG >0) {

ammoMG -= 1; } }

function ammoPickedUp (ammoPickUp : int) { if (ammoMG <= 200){ ammoMG += ammoPickUp; } }

function OnCollisionEnter (col : Collision) { col.gameObject.BroadcastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); }

function OnGUI () {

GUI.Label (Rect (20, 20, 100, 20),"MGammo:" + ammoMG ); }

Now I just can't see why the pick up massage over rides my max ammo count? Basically from what I can understand I'm send a "plus 10 message" and it adds 10, but its adding more then the maximum of 200. I so lost on how to make it that the ammo will only take what it needs to be 200 and no more.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Piflik · May 05, 2012 at 11:08 AM

This should work:

 function ammoPickedUp(ammoPickUp : int) {
     ammoMG += ammoPickUp;
     if ammoMG > 200
         ammoMG = 200;
 }

You should only call the function, if the ammo counter is below 200, so the player cannot accidentally destroy ammo packs when he is already full.

Also, please format your code correctly next time.

Comment
Add comment · 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
0

Answer by Flynn · May 04, 2012 at 11:18 PM

 function ammoPickedUp (ammoPickUp : int) { if (ammoMG <= 200){ ammoMG += ammoPickUp; } }

Let's walk through this.

If you have 199 bullets left, and you ask it to add ten, then first, it will check if 199 is below 200. 199 is. It will then add 10, resulting in 209 bullets.

To fix this, instead write your function like this:

 function ammoPickedUp (ammoPickUp : int)
 {
     ammoMG += ammoPickUp;
     ammoMG += 200 - ammoMG;
     //if ammoMG is ten higheer than 200, 200-210 will result in negative ten. Adding a negative number causes it to go down by ten. Making it 200
 }
Comment
Add comment · 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
0

Answer by VGeorgeV · May 06, 2012 at 02:06 AM

Thanks i missed with the input and came to the fallowing.

 function ammoPickedUp(ammoPickUp : int) {
 ammoMG += ammoPickUp;
 if (ammoMG > 200) {
     ammoMG = 200;

}

Comment
Add comment · 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
0

Answer by VGeorgeV · May 06, 2012 at 02:06 AM

All so sorry for the code formatting issue, It seems you gata post, stop, space then post code. I didn't space stuff out proper.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

gui help for ammo display 1 Answer

skip guns with no ammo left 1 Answer

ammo cache pickup 1 Answer

Sleep Syntax Unity 1 Answer

Help Needed with Shotgun script (SOLVED) 0 Answers


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