Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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.