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 Kaha · Nov 15, 2012 at 09:42 PM · bulletgunreload

Help on a pick up/reload script

Hi,

I'm trying to make a game were the player needs to find a bullet and a gun to shoot an enemy. So I have tried to write a reload script which is attached to the Player and goes like this:

 var canReload = false;
 var carryingGun = false;
 var carryingBullet = false;
 var collisionGun : Collider;
 var collisionBullet : Collider;
 
 var reloadTime = 3;
 var bullets = 1;
 
 var pickupGunSound : AudioClip;
 var pickupBulletSound : AudioClip;
 
 
 function OnCollisionEnter(collision : Collision)
 {
     if (collisionGun.gameObject.tag == "gun");    // collide with the Gun...
         carryingGun = true;                        // ...to pick it up
         audio.PlayOneShot(pickupGunSound);
         Debug.Log( "YOU HAVE THE GUN" );        // "You have the gun. Find the bullet"
 
     if (collisionBullet.gameObject.tag == "bullet")    // collide with the Bullet...
         carryingBullet = true;                        // ...to pick it up
         audio.PlayOneShot(pickupBulletSound);
         Debug.Log( "YOU HAVE THE BULLET" );            // "You have the bullet. Find the gun"
 }
 
 
 function Reload ()
 {
     if (carryingGun == true)
         canReload = true;
         Debug.Log( "YOU CAN RELOAD" );    
 
     if (carryingBullet == true)
         canReload = true;                // ...you can reload
         Debug.Log( "YOU CAN RELOAD" );    // "You have the bullet and the gun. Reload now (press R)!"
 
         
     if(canReload == true)
         if (Input.GetKeyDown("R")) 
          {         
              yield WaitForSeconds (reloadTime);    // wait 3 seconds
              bullets = 1;                        // there is one bullet in the Gun
              Debug.Log( "THE GUN IS LOADED" );    // "The Gun is Loaded. Shoot the enemy !"   
          }
 }

So the console sends no errors back and I get the debug.log for carryingGun=true and carryingBullet=true, but I can"t get the canReload=true and reload state.

Please, can you correct my code or help me see what I'm missing here. 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Nov 16, 2012 at 01:00 AM

i'm not certain but dont you need to bracket that since its more than one line?

You shouldnt use 2 if statements like that anyways you should AND them together.

 if(CarryingGun && CarryingBullet) //the == true is implicit you dont need to type it
 {
   CanReload = true;
 }
 
 if(CanReload && Input.GetKeyDown("R"))
 {
   yield WaitForSeconds(reloadTime);
   bullets = 1;
 }
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 Kaha · Nov 16, 2012 at 10:29 AM

Hi,

 if(CarryingGun && CarryingBullet) //the == true is implicit you dont need to type it
 {
   CanReload = true;
 }

is working great, thanks ! But for

 if(CanReload && Input.GetKeyDown("R"))
 {
   yield WaitForSeconds(reloadTime);
   bullets = 1;
 }

I get errors in the console, so I'll stay with the double if statements since it's working here.

Anyway, thanks for sharing your knowledge !

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 PAHeartBeat · Nov 17, 2012 at 08:45 AM

HI Kaha

Please check you components which are using in your player gameObject if you are using Character controller then you can't access directly OnCollisionEnter Event you need to use "`OnControllerColliderHit`" Event

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 Kaha · Nov 17, 2012 at 12:38 PM 0
Share

Hi Ranpariya Ankur, I will check if this is the reason why my Gun and Bullet collisions are glitchy.

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

11 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

Related Questions

Ammo Script 1 Answer

How can I shooting script c#? 0 Answers

c# weapon shooting script using raycasting help 1 Answer

reload gun with bullets 2 Answers

bullet prefab always come out in wrong direction 2 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