hello everybody
i made a 3d keypad system, at first when i only had one button it worked perfectly but now i added 9 more buttons and whenever i input something it automatically says ‘incorrect code’
i have 10 button objects and each has keypadbutton script attached
this is the button script
using UnityEngine;
public class NewKeypadButton : MonoBehaviour
{
public Camera playerCamera;
public LayerMask raycastLayerMask;
public float raycastDistance = 5f;
public AudioSource click;
public int buttonValue; // Assign the value for this button in the Inspector.
private NewKeypadController keypadController; // Assign the KeypadController reference in the Inspector.
private void Start()
{
keypadController = GetComponentInParent<NewKeypadController>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("E key pressed.");
Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, raycastDistance, raycastLayerMask))
{
if (hit.collider.CompareTag("Button"))
{
Debug.Log("Button pressed.");
keypadController.AcceptInput(buttonValue);
click.Play(); // Play a click sound
}
else
{
Debug.Log("Button not detected.");
click.enabled = false;
}
}
}
else
{
click.enabled = false;
}
}
}
and this is the keypad controller script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewKeypadController : MonoBehaviour
{
public Animator dooranim;
private bool opened = false;
[Header("Unlock code")]
public int[] code;
[Header("Current input code")]
public int[] currentCode;
int currentIndex = 0;
[Header("Temp input controls")]
public int input;
public bool sendInput;
private void Start()
{
ResetCurrentCode();
}
private void Update()
{
if (sendInput)
{
sendInput = false;
AcceptInput(input);
}
}
public void AcceptInput(int value)
{
currentCode[currentIndex] = value;
currentIndex += 1;
if (currentIndex >= code.Length)
{
for (int i = 0; i < code.Length; i++)
{
if (code[i] != currentCode[i])
{
Fail();
return;
}
}
Success();
}
}
void Fail()
{
Debug.Log("incorrect code");
ResetCurrentCode();
}
void Success()
{
Debug.Log("correct code");
dooranim = GetComponent<Animator>();
dooranim.SetBool("Opened", true);
Debug.Log("well done:)");
ResetCurrentCode();
}
void ResetCurrentCode()
{
currentIndex = 0;
currentCode = new int[code.Length];
for (int i = 0; i < currentCode.Length; i++)
{
currentCode[i] = -1;
}
}
void Opendoor()
{
dooranim = GetComponent<Animator>();
opened = !opened;
dooranim.SetBool("Opened", !opened);
Debug.Log("well done:)");
}
}
thanks for your help in advance