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 HahnSolo157 · Feb 04, 2014 at 05:07 AM · functionreloading

Reloading Function in Script not doing anything.

My game runs and I don't get any errors of any kind, but I can't seem to get my reloading function to work. Nothing happens when I press the "r" key. I think it has to do with me calling the reload function in a spot the game can't detect, but I googled my problem dozens of times and I can't come up with why it wouldn't work calling it in the Update. Any advice/suggestions would be appreciated.

 var speed = 10;
 var maxbullets : int = 6;
 var totalBullets: int = 12;
 var reloadtime: int = 2;
 var transfer : int = 6 - maxbullets;
 
 function Update() {
     Fire(); 
     
     Reload(); 
     
 }
 
 function Fire() { 
     if (maxbullets > 0); 
     
         if(Input.GetButtonDown("Fire1"))
 {
 clone = Instantiate(projectile, transform.position, transform.rotation);
 clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
 
 Destroy(clone.gameObject, .25);
 
 maxbullets -= 1; 
 GameObject.Find("BulletCount").guiText.text = ""+maxbullets;
 
     }
 }
 
 function Reload() { 
 
     if(totalBullets > 0);
         
     if (transfer > totalBullets) { transfer = totalBullets; }
 
         if(Input.GetKeyDown("r"))    
         
             yield WaitForSeconds (reloadtime);
               animation.Play("RevolverReloadAnimation");
     {
     
     maxbullets += transfer;
     totalBullets -= transfer;
     
     GameObject.Find("TotalBullets").guiText.text = ""+totalBullets;
 
     }
 }

Thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GameVortex · Feb 04, 2014 at 07:41 AM

The curly braces for the if check with the Input.GetKeyDown("r") is not correctly set up. Without the curly braces the only thing happening when you hit the button is the yield statement. The animation.Play is running each update which means the animation begins at the beginning every frame, and therefore you do not see anything happening.

You need to move the starting curly brace from line 40. to line 37, and you would probably want to play animation before the yield or nothing will happen before the reload time is up.

Comment
Add comment · Show 4 · 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 HahnSolo157 · Feb 04, 2014 at 08:13 PM 0
Share

I did everything you suggested and I also tried moving the if(Input.Get$$anonymous$$eyDown("r")) to the update function, none of it seemed to work. thanks for the advice on the braces though, I guess I didn't see the problem there.

avatar image GameVortex · Feb 05, 2014 at 07:26 AM 0
Share

Then have you set up the "r" key in the **Input$$anonymous$$anager**? Any keys you try to get through a string name needs to be set up there.

Try just for testing to use Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.r) ins$$anonymous$$d. Also put a debug.log statement inside the if check to make sure that it is working.

avatar image FLASHDENMARK · Feb 05, 2014 at 10:44 AM 0
Share

I might be wrong but Get$$anonymous$$eyDown("r") doesn't have to be setup in the Input $$anonymous$$anager.

avatar image GameVortex · Feb 05, 2014 at 11:42 AM 0
Share

You are correct, my bad, I was of course thinking about ButtonDown A debug after the input would still probably be wise to check if the input is detected properly.

avatar image
0

Answer by Nanobrain · Feb 04, 2014 at 06:49 AM

 if(Input.GetKeyDown("r")) 

should go in update(). Your input check is never being called. That is unless Reload() is being called elsewhere in your code, and on every game loop.

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 FLASHDENMARK · Feb 04, 2014 at 07:46 AM 1
Share
 function Update() {
     Fire(); 
  
     Reload(); 
  
 }

Reload() is being called every frame, so in theory that should work just fine.

avatar image Nanobrain · Feb 04, 2014 at 06:39 PM 0
Share

OrangeLightning, very true, very true. I overlooked that fact.

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

22 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

Related Questions

is it possible to use a rotation on a Vector3? 3 Answers

accessing a function? 1 Answer

How do I make a callback run? 1 Answer

Weirdness.. Why does putting yield in function corrupt commands? 1 Answer

problem with calling a function from THIS script, on an object that I find with THIS script. 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