Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Problem with a function not being called

Discussion in 'Scripting' started by GiorgiNinua, Sep 10, 2023.

  1. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10
    Hello unity community

    I’ve made a 3D keypad system that works perfectly fine. When you input the right code it says that it’s correct. But recently i faced an issue, I made a opendoor() function (line 79) that is supposed to play an animation when code is correct, but it didn’t work, after some debugging i can say that the opendoor() function specifically is not being called.
    Is there something wrong with the script?
    Thanks in advance
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewKeypadController : MonoBehaviour
    6. {
    7.     public Animator dooranim;
    8.     private bool opened = false;
    9.  
    10.     [Header("Unlock code")]
    11.     public int[] code;
    12.  
    13.     [Header("Current input code")]
    14.     public int[] currentCode;
    15.  
    16.     int currentIndex = 0;
    17.  
    18.     [Header("Temp input controls")]
    19.     public int input;
    20.     public bool sendInput;
    21.  
    22.     private void Start()
    23.     {
    24.         ResetCurrentCode();
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         if (sendInput)
    30.         {
    31.             sendInput = false;
    32.             AcceptInput(input);
    33.         }
    34.     }
    35.  
    36.     public void AcceptInput(int value)
    37.     {
    38.         currentCode[currentIndex] = value;
    39.  
    40.         currentIndex += 1;
    41.  
    42.         if (currentIndex >= code.Length)
    43.         {
    44.             for (int i = 0; i < code.Length; i++)
    45.             {
    46.                 if (code[i] != currentCode[i])
    47.                 {
    48.                     Fail();
    49.                     return;
    50.                 }
    51.             }
    52.             Success();
    53.         }
    54.     }
    55.  
    56.     void Fail()
    57.     {
    58.         Debug.Log("incorrect code");
    59.         ResetCurrentCode();
    60.     }
    61.  
    62.     void Success()
    63.     {
    64.         Debug.Log("correct code");
    65.         opendoor(); // Move opendoor() here
    66.         ResetCurrentCode(); // Reset currentCode after opening the door
    67.     }
    68.  
    69.     void ResetCurrentCode()
    70.     {
    71.         currentIndex = 0;
    72.         currentCode = new int[code.Length];
    73.         for (int i = 0; i < currentCode.Length; i++)
    74.         {
    75.             currentCode[i] = -1;
    76.         }
    77.     }
    78.  
    79.     void opendoor()
    80.     {
    81.         dooranim = GetComponent<Animator>();
    82.         opened = !opened;
    83.         dooranim.SetBool("Opened", !opened);
    84.         Debug.Log("well done:)");
    85.     }
    86. }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If you're saying you're seeing "correct code" print in the console, but not "well done", then the only difference you have is having your function of "opendoor" not start with a capital letter. I can't remember for the life of me if a capital letter matters or not with a function call, as I've never not done that, so test that for one.

    Also your opened bool section within that doesn't look right, but I'm not sure how that works. So once you get the function being called again, you may have an issue there as well, with the animation not playing correctly.