- Home /
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;
}
}
Answer by Hellium · Mar 02, 2021 at 11:01 PM
You actually did the hardest part.
The last remaining part to do is:
Create a new script you attach on a dedicated gameObject
Declare an array of
Interactable
Listen to the
interactAction
event of every element of the arrayWhen 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");
}
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!!
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);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Too many bugs 0 Answers
Update, FixedUpdate, and deltaTimes... 1 Answer
Which programming language to learn first to program in Unity? 0 Answers
How to use additionalVertexStreams? 1 Answer