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 tayyab43 · Feb 06, 2014 at 08:11 PM · gui

Wierd GUI problem

I made a small shoot script but for some reason when i try to use a gui for ammo it doesn't update properly.I have 36/360 but when i shoot it does update but 36 remains there so we have 36 on top of 35. Whats going on? heres my code:

 var FullAmmo : int = 360;
 public var Clip : int = 36;
 var ClipLeft : int = FullAmmoClip - Clip;
 private var nextbulletTraceTime : float;
 var ammo :GUIText;
 function Update () {
     ClipLeft = FullAmmoClip - Clip; 
     accuracy = Mathf.Clamp01(accuracy); 
     
     if (Input.GetKeyDown(KeyCode.R))
             {
              
             FullAmmo-=ClipLeft;
             Clip = 36;
                 
             }
     if (Clip<=0)
     {
     on = false;
     }     
     if (on){
         if(Time.time > nextbulletTraceTime){
             rate = Mathf.Max(rate, 1.0);
             nextbulletTraceTime = Time.time + (1.0 / rate);
             var newBulletTrace : GameObject = Instantiate(bulletTracePrefab,transform.position,transform.rotation);
             Clip = Clip - 1;
             var bulletVelocity : Vector3 = newBulletTrace.GetComponent("bulletTrace").velocity;
             var badAim : float = (1-accuracy);
             badAim *= newBulletTrace.GetComponent("bulletTrace").bulletSpeed * 0.05;
             bulletVelocity += newBulletTrace.transform.right * Random.Range(-badAim,badAim);
             bulletVelocity += newBulletTrace.transform.up * Random.Range(-badAim,badAim);
             newBulletTrace.GetComponent("bulletTrace").velocity = bulletVelocity;
         
         }
     }
 
 }
 
 function OnGUI()
 {
 
 //GUI.Box(Rect(Screen.width - 100,Screen.height - 50,100,50),("Clip");
 //GUI.Box (Rect (10,10,100,90),"" + Clip.ToString() +"/"+FullAmmo.ToString()+"");
 GUILayout.Label( "Ammo " + Clip.ToString()+"/"+ FullAmmo.ToString());
 }
Comment
Add comment · Show 4
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 tayyab43 · Feb 07, 2014 at 10:57 PM 0
Share

Any solution anyone?

avatar image APenguin · Feb 07, 2014 at 11:04 PM 0
Share

is clip being kept as 36 onkeydown?

avatar image APenguin · Feb 07, 2014 at 11:05 PM 0
Share

also do you mean that, the 360 part is the only part which decreases and not the 36. So what you want is the 36 to update? Just so i can get a good view on what you want :)

avatar image APenguin · Feb 07, 2014 at 11:12 PM 0
Share

In addition could you please put up fullammoclip variable declaration. Then i can do it for you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by APenguin · Feb 07, 2014 at 11:26 PM

Okay you had quite a few variables which weren't really needed. So i took them out. Maybe next time when you do coding, which is great you are doing yourself, think it through what is happening. But here is the change which i made. I hope it is what you are after:

  if (Input.GetKeyDown(KeyCode.R))
          {
              Clip -= 1;
          //FullAmmo-=ClipLeft;
          //clip = 36;
  
          }
        
     if (Clip<=0)
     {
     Clip = 36;
     FullAmmo -= 1;
     }   

Basically what happens here is that on r down only the Clip gets minus by 1. When Clip <= 0 then reset the Clip back to 36, and minus FullAmmo by 1.

Comment
Add comment · Show 9 · 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 tayyab43 · Feb 08, 2014 at 09:04 AM 0
Share

thanks, but my problem wasn't with tht code, i debugged it and it was working perfectly, it was the GUI wasn't showing the ammo correctly

avatar image tayyab43 · Feb 08, 2014 at 09:05 AM 0
Share

it was updating but say I shot, it would $$anonymous$$us one and 35 would show but so would 36 so they would keep building up

avatar image APenguin · Feb 08, 2014 at 12:25 PM 0
Share

I tried the code i answered with, and it worked perfectly. So when i pressed R, it would show 35/360, then all way to 1/360. Once i reached 0 it would go to 36/359. I didn't see any 36 with the 35 or any other number

avatar image APenguin · Feb 08, 2014 at 12:42 PM 0
Share

Oh in fact, take out Clip = 36 when the key R is down. Because that keeps Clip at 36 when you shoot which shouldn't be right.

avatar image tayyab43 · Feb 08, 2014 at 02:04 PM 0
Share

so what about reload?

Show more comments

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

19 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

Related Questions

GuiTexture Width Change 1 Answer

Feast or Famine with GuiText 1 Answer

How to make a simple inventory?!? Beginner coder JS 1 Answer

GUI text Stays in place 1 Answer

Placing a pre existing script into a pause menu 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