• 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 rOBY GAMES · May 03, 2015 at 09:24 AM · countsciptcss

Disable item after 10 shots.

Hello how can I disable an object after you press 10 times the left-muose? This is the script that I'm using, but does not work very well.

My script.

 #pragma strict
 
 var myTrigger : GameObject;
 var myObject : GameObject;
 var countAmmo : int = 10 ;
 
 
 function Start()
 {
                 
 }
  
  function Update()
  {
      
   if(Input.GetButtonDown("Fire1"))
   
      countAmmo = 10;
      
         //myObject.SetActive(true);
         
         countAmmo = (countAmmo -1);
         
          countAmmo = 0; 
         
         myObject.SetActive(false);
      
     
  }
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
2

Answer by Eno-Khaon · May 03, 2015 at 09:40 AM

You need to become more comfortable with your use of if statements.

As it stands, here's what's happening:

You click, your ammo is set to 10. But that's all your if statement is doing. Then, every frame, you subtract 1 from your current ammo (so, either 10 becomes 9 or 0 becomes -1), then you set it straight to 0.

You're already setting your ammo count to 10 in the editor. When you click/fire, reduce your current count by 1 (i.e. "countAmmo -= 1" or "countAmmo = (countAmmo - 1)" as you already did), then check whether you have any ammo left.

 ...
 
 if(Input.GetButtonDown("Fire1"))
 {
      if(countAmmo > 0)
      {
           // Instantiate a projectile or whatever the means is to fire.
           countAmmo -= 1;
      }
      else // if(countAmmo <= 0) -- Commented out because it's unnecessary, but there to show intent
      {
           // You have no ammo!
      }
 }
 
 ...
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 rOBY GAMES · May 03, 2015 at 10:21 AM 0
Share

Ti ringrazio questo funziona.

Grazie per il tuo lavoro.

I would also add the count with GUIText how could I add? thank you

avatar image
0

Answer by Diogo_Geada · May 04, 2015 at 10:38 AM

The important part is in here, now u must adapt it

if(Input.GetButtonDown("Fire1")){//checks for input

   countAmmo--;//count ammo - 1
   if(countAmmo <= 0){//if cound ammo is less or iqual to 0
      myObject.SetActive(false);//turn off object
   }

ps: You should pratice a little more your programing skills

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Countdown little help...! 1 Answer

Array.Count returning 0 out of the foreach and 4 inside 1 Answer

List.Count returning 0 while it has 1 entry 2 Answers

count the number on enemyes entrering into a trigger 1 Answer

Count selected item in List 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