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
-1
Question by UNITYGAME MaKer · Jun 22, 2013 at 07:47 PM · javascriptshootinghudammo

Repeatedly Getting Errors On An Ammo Script

I added an ammo part to a javascript that I found that makes a Bullet move forward. I am trying to make it so that the player has a certain amount of ammo and when he shoots his ammo goes down one. When the player runs out of ammo he can't shoot for 10 seconds and then he gets al of his ammo back. I am also trying to make his current ammo be shown on the HUD. I have been working at this script for a while and new problems keep coming up and I can't figure out what is wrong. Here is my script...

 var projectile : Rigidbody;
 public var speed = 10;
 public var ammo = 5;
 public var maxAmmo = 5;
 public var reloadTime = 10;
 function Update () {
 //if there is enough ammo and the player presses left click
 if (ammo > 0) {
 
     if (Input.GetButtonDown ("Fire1")){
     
         clone = Instantiate (projectile, transform.position, transform.rotation);
     
         clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
     
         Destroy (clone.gameObject, 3);
     //then the gun fires making the bullet move forward
     
         ammo = ammo - 1; // and then the computer subtracts one (1) from the ammo variable
         };
     };
     
 else {
     WaitForSeconds (reloadTime);
     ammo = maxAmmo;
     };
 GUI.Box (Rect (Screen.width - 100,0,100,50), ammo.ToString());
 };


The error I am getting is:

Script error: Update() can not be a coroutine.

Comment
Add comment · Show 1
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 nastasache · Jun 22, 2013 at 10:20 PM 0
Share

When you say 'Getting Errors', please say what errors.

3 Replies

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

Answer by nastasache · Jun 23, 2013 at 01:47 PM

Try this:

 public var projectile : Rigidbody; 
 public var speed = 10; 
 public var ammo = 5; 
 public var maxAmmo = 5; 
 public var reloadTime = 10; 
 
 function Update () { 
     
     if (ammo > 0) {
 
         if (Input.GetButtonDown ("Fire1")){
      
             clone = Instantiate (projectile, transform.position, transform.rotation);
          
             clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
          
             Destroy (clone.gameObject, 3);
             
             ammo = ammo - 1; 
         }
         
     } else { 
         
         WaitReload();
 
     } 
         
 }
 
 function WaitReload() {
     yield WaitForSeconds (reloadTime); 
     ammo = maxAmmo; 
 }
 
 function OnGUI () {
     GUI.Box (Rect (Screen.width - 100,0,100,50), ammo.ToString()); 
 }

  • All things on GUI have to be inside of OnGUI() function, not inside of Update() function

  • Since you can't use yield WaitForSeconds () (subroutine) in Update() function, move it to separated function (aka WaitReload() ) and call that function from Update() function.

  • Don't use syntax like: "}; else {" - see ";"

Better, instead of yield, use a timer:

     public var projectile : Rigidbody; 
     public var speed = 10; 
     public var ammo = 5; 
     public var maxAmmo = 5; 
     public var reloadTime = 10; 
     private var timer = 0.0;
     
     function Update () { 
         
         if (ammo > 0) {
             if (Input.GetButtonDown ("Fire1")){
          
                 clone = Instantiate (projectile, transform.position, transform.rotation);
              
                 clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
              
                 Destroy (clone.gameObject, 3);
     
                 ammo = ammo - 1; 
                 
             }
             
         } else {
             
             Reload();
         } 
             
     }
     
     function Reload () {
     
         if(timer < reloadTime) {
             
             timer += Time.deltaTime;
         
         } else {
             
             timer = 0.0;
         } 
         
         if(timer >= reloadTime) {
             
             ammo = maxAmmo;
         }
     }
     
     function OnGUI () {
         GUI.Box (Rect (Screen.width - 100,0,100,50), ammo.ToString()); 
     }

See: http://answers.unity3d.com/questions/447835/reloading-script-1.html

Comment
Add comment · Show 2 · 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 UNITYGAME MaKer · Jun 23, 2013 at 01:58 PM 0
Share

Thank You. That fixed the errors

but now I shoot five bullets. It reloads. and then I can shoot as many as I want. How can I make it so that the "yield WaitForSeconds (reloadTime);" part repeats. Right now it only works once and then I can shoot as many bullets as I want

Thanks,

avatar image UNITYGAME MaKer · Jun 24, 2013 at 09:34 PM 0
Share

Thank You!

avatar image
0

Answer by kubci98 · Jun 23, 2013 at 07:53 AM

  1. add "yield" in front of WaitForSeconds

  2. you can change ammo = ammo-1 to ammo -=1

That's all i found. If you need more help, write what errors you get.

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 FLASHDENMARK · Jun 23, 2013 at 09:31 AM 0
Share

Also deleting all of the semi-colons after a curly bracket might be a help as well.

avatar image
0

Answer by chillersanim · Jun 23, 2013 at 01:28 PM

From the error message it looks like you missplaced the Update() method in the wrong place. It needs to be inside a class and not inside an other method or outside the class.

 class Abc  
 {
    void Update()
    {
       // Code comes here
    }
 }
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

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

Gun shooting and ammo problem 1 Answer

Enemy not getting hit (Collider issue) 2 Answers

Projectile Firing/Gun script 1 Answer

im having trouble with raycast shooting script 0 Answers

reloading script problem 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