Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Raijinn-sama · Mar 02, 2021 at 10:41 PM · programmingcounterinteractablecopies

Count keypress(E) on objects/copies

Hi i want to know if/how i can count how many times i have pressed E on objects, after have pressed E 32 times in total, change scene.

I have set up my scene like this

Object A have 7 copies spread all over the scene but the problem is each object have their own counter, i want to have a universal counter that check if i have pressed all E on all the objects 32 times in total and if i have then change scene to my winScene. I have my script on a empty gameobject thats on Object A and the rest of the copies are the same

thanks in advance

Code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.SceneManagement;
 
 public class Interactable : MonoBehaviour
 {
     public GameObject Button;
     public bool isInRange = false;
     public KeyCode interactKey;
     public UnityEvent interactAction;
     int counter = 0;
     public bool hasBeenPressed;
     int keyPressed = 0;
     
 
     // Start is called before the first frame update
     void Start()
     {
         hasBeenPressed = false;
         isInRange = false;
         counter = 0;
     }
 
     // Update is called once per frame
     public void Update()
     {
         if (isInRange)
         {       
             if (Input.GetKeyDown(interactKey) && counter < 4)
             {
                 Debug.Log("thing");
                 counter++;
                 hasBeenPressed = true;
                 interactAction.Invoke(); Debug.Log("Button Is pressed" + counter);
             }
 
         }
     }
 
     private void OnTriggerEnter(Collider collision)
     {
             isInRange = true;
     }
 
     private void OnTriggerExit(Collider collision)
     {
             isInRange = 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

1 Reply

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

Answer by Hellium · Mar 02, 2021 at 11:01 PM

You actually did the hardest part.

The last remaining part to do is:

  1. Create a new script you attach on a dedicated gameObject

  2. Declare an array of Interactable

  3. Listen to the interactAction event of every element of the array

  4. When the event is raised, call a function checking the hasBeenPressed of the interactables. If all are true, change scene


Code not tested

     public Interactable[] Interactables; // Fill inspector
     
     void Start()
     {
         for(int i = 0 ; i < Interactables.Length ; ++i)
              Interatables[i].interactAction.addListener(CheckAllPressed);
     
     }
     
     void CheckAllPressed()
     
     {
         for(int i = 0 ; i < Interactables.Length ; ++i)
              if(Interatables[i].hasBeenPressed ==
 false)
                   return;
         UnityEngine.SceneManagement.SceneManager.LoadScene("Scene");
     }

Comment
Add comment · Show 3 · 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 Raijinn-sama · Mar 03, 2021 at 06:19 PM 0
Share

Thank You SO MUCH it really helped!!

the only thing i need to do now is change that after i press E 4 times then the bool is true right now its true after the first time i pressed E

If u know quick obvious way that i cannot see to change that in the code feel welcomed to anwser

again Thank you!!

avatar image Hellium Raijinn-sama · Mar 03, 2021 at 06:32 PM 1
Share

Again, code not tested:

          if (Input.GetKeyDown(interactKey) && counter < 4)
          {
              Debug.Log("thing");
              counter++;
              if(counter == 4)
              {
                   hasBeenPressed = true;
              }
              interactAction.Invoke(); Debug.Log("Button Is pressed" + counter);
          }
avatar image Raijinn-sama · Mar 03, 2021 at 06:36 PM 0
Share

bro it worked, Thank you SO MUCH!!

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

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

Related Questions

Multiple Cars not working 1 Answer

Run time counter 1 Answer

Only tagged objects raise counter 1 Answer

Making something happen only for one second during an Update function? 1 Answer

Creating an up and down counter 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