Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 IamJarek · Aug 11, 2017 at 09:45 PM · scripting problembuttonclick

Destroy object after clicking button few times.

I want to write a script that will destroy a tree when three is clicked 3 times but script is stopping on "hp = 3". Thank you for help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SCINANIE : MonoBehaviour
 {
     public Animator animacja;
     public Animator anim;
     public Canvas text;
     public Canvas textno;
     private bool intree;
     public Canvas A, B, C, D;
     public GameObject axe, player, drzewo;
 
     // Use this for initialization
 
     void Start()
     {
         text.enabled = false;
         textno.enabled = false;
         intree = false;
         animacja.enabled = false;
         A.enabled = false;
         B.enabled = false;
         C.enabled = false;
         D.enabled = false;
         anim.enabled = false;
         axe.SetActive(true);
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
     }
 
 
 
     void OnTriggerStay(Collider other)
     {
         var item = player.GetComponent<items>();
         if (other.tag == "Player")
         {
             if (item.axe == true)
             {
                 text.enabled = true;
                 textno.enabled = false;
                 intree = true;
                 Debug.Log("In trigger.");
                 if (Input.GetKeyDown(KeyCode.Mouse0))
                 {
                     Invoke("one", 0.1f);
                     textno.enabled = false;
                 }
                 else
                 {
                     text.enabled = true;
                     textno.enabled = false;
                 }
             }
             else
             {
                 text.enabled = false;
                 textno.enabled = true;
             }
         }
     }
 
     void one()
     {
         A.enabled = false;
         B.enabled = false;
         C.enabled = true;
         D.enabled = false;
         Debug.Log("hp = 3");
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Invoke("two", 0.1f);
         }
     }
 
 
     void two()
     {
         A.enabled = false;
         B.enabled = true;
         C.enabled = false;
         D.enabled = false;
         Debug.Log("hp = 2");
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Invoke("three", 0.1f);
         }
     }
 
 
     void three()
     {
         A.enabled = true;
         B.enabled = false;
         C.enabled = false;
         D.enabled = false;
         Debug.Log("hp = 1");
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Invoke("four", 0.1f);
         }
     }
 
 
     void four()
     {
         A.enabled = false;
         B.enabled = false;
         C.enabled = false;
         D.enabled = false;
         Debug.Log("hp = 0");
         if (Input.GetKeyDown(KeyCode.Mouse0))
         {
             Invoke("six", 0.1f);
         }
     }
 
     void six()
     {
         A.enabled = false;
         B.enabled = false;
         C.enabled = false;
         D.enabled = false;
         animacja.enabled = true;
         anim.enabled = false;
         text.enabled = false;
         textno.enabled = false;
         Invoke("Cut", 9.7f);
     }
 
 
 
 
     void OnTriggerExit(Collider other)
     {
         intree = false;
         text.enabled = false;
         textno.enabled = false;
     }
     void Cut()
     {
         Destroy(gameObject);
     }
 }
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
0
Best Answer

Answer by TwinGhosts · Aug 11, 2017 at 10:25 PM

You are trying to check a keyPress while the method is only called once by the Invoke(), you'd need to press the key the exact moment when the invoke is happening right now.

You could fix this by giving the object a general HP value and decrease this with each mouseClick. You could also execute more code for each HP value in this way as well.

An example:

     if (Input.GetMouseButtonDown(0) && hp > 0){
         hp--;
     }
     
     switch(hp){
         case 0:
          // Death actions here
         break;
     
         case 1:
          // Actions here
         break;
     
         case 2:
          // Actions here
         break;
     
         //ETC.
     }

Or you could make a click counter and increment it each time the player presses the mousebutton at the object's position, like so:

 int clickCount = 0;
 
 if(Input.GetMouseButtonDown(0)){
  clickCount++;
 }
 
 if(clickCount >= 3){
  //Do stuff
 }

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

117 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

mouse clicked on object animation c# 1 Answer

create a button with name and text for every game object with tag 0 Answers

Scripting button functionality inside class with created object's method 1 Answer

Dynamic Buttons, SciptableObjects and Lists 1 Answer

Alerts 3D game mobile controls with UI 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