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 Dreave · Dec 18, 2011 at 10:49 AM · reloading

Reloading wont work

I have put this script together from various other scripts and it works ok until I run out of ammo and I reload. It dosent reload, I cant really say anything else it just dosent reload, can anyone help me?

 var amountOfShots = 8;
 var reloadTime = 1.5;
 var refireRate : float = 0.1;
 
 function ShotPoller()
 {
     while(true)
     {
         if(Input.GetButton("Fire1"))
         {
             Shoot();
             yield WaitForSeconds(refireRate);
         } else {
             yield;
         }
     }
 }
 
 function Start()
 {
     StartCoroutine(ShotPoller());
 }
     
 if(Input.GetKeyDown("r")){
     Reload();
 }
 
 function Reload (){
     yield WaitForSeconds(reloadTime);
     amountOfShots = 8;
 }
 
 var shotSound : AudioClip;
 var bloodPrefab : Transform;
 var sparksPrefab : Transform;
 var hit : RaycastHit;
 var range = 500;
 
 function Shoot (){
     if(amountOfShots > 0){
         amountOfShots--;
         if(shotSound){
             audio.PlayOneShot(shotSound);
         }
         if (shotSound) audio.PlayOneShot(shotSound); 
         var hit: RaycastHit;
         if (Physics.Raycast(transform.position, transform.forward, hit)){
             var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
             if (hit.transform.tag == "Enemy"){ 
                 if (bloodPrefab) Instantiate(bloodPrefab, hit.point, rot); 
                 hit.transform.SendMessage("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver); 
             } else { 
                 if (sparksPrefab) Instantiate(sparksPrefab, hit.point, rot);
             }
         }
     }
 }
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

1 Reply

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

Answer by syclamoth · Dec 18, 2011 at 11:31 AM

You need an 'Update' somewhere in there. As it is, (because of Javascript weirdness, in a sane language this wouldn't even compile but that's a whole different rant), you are checking for the 'reload' button in 'Awake'- which executes exactly once, and only before the game even begins. You should wrap that into an 'Update' function-

 function Update()
 {
     if(Input.GetKeyDown("r")){
         Reload();
     }
 }

Or, better still- put it into your shoot poller!

 while(true)
 {
     if(Input.GetButton("Fire1"))
     {
         Shoot();
     }
     if(Input.GetKeyDown("r")){
         Reload();
     }

     yield;
 }

Then, rejig shoot a little to put the 'WaitForSeconds' into that, and call 'Reload' if it's out of bullets!

 function Shoot (){
     if(amountOfShots > 0)
     {
         // do all of the shooting which already works
         yield WaitForSeconds(refireRate);
     } else {
         Reload();
     }
 }
Comment
Add comment · Show 6 · 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 Dreave · Dec 18, 2011 at 01:01 PM 0
Share

I have done this but now I have this problem that it shoots a ray every frame which I do not want can you help?

avatar image syclamoth · Dec 18, 2011 at 01:16 PM 0
Share

Did you remember to include the 'WaitForSeconds' in 'Shoot'? It's important. Unfortunately, they syntax for properly using Coroutines in Javascript is a little fuzzy- I'd be able to help you further if you were using C# and I could specify exactly what gets executed as a coroutine and what executes immediately just from the function declarations.

avatar image Dreave · Dec 18, 2011 at 01:57 PM 0
Share

Ive got it working now I accidentally removed the yield wait for second like you said, thanks for all your help!

avatar image Bunny83 · Dec 18, 2011 at 02:43 PM 0
Share

Well, you have to yield the two coroutines as well to make your loop wait for the subcoroutine to complete ;)

 if(Input.GetButton("Fire1"))
 {
     yield StartCoroutine(Shoot());
 }
 if(Input.Get$$anonymous$$eyDown("r"))
 {
     yield StartCoroutine(Reload());
 }

I'm not sure if you need StartCoroutine. I don't use UnityScript ;)

avatar image syclamoth · Dec 18, 2011 at 03:12 PM 0
Share

You don't need it. This is why I use C# for everything- it's a little bit confusing having the compiler just guess what you want it to do.

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

6 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

A node in a childnode? 1 Answer

Stop animation when no more bullets 1 Answer

Unity sniper scope tutorial? 2 Answers

C# Throw GameObject Upon Mouse Click 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