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. Dismiss Notice

Question two scripts not working together

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

  1. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10
    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
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NewKeypadButton : MonoBehaviour
    4. {
    5.     public Camera playerCamera;
    6.     public LayerMask raycastLayerMask;
    7.     public float raycastDistance = 5f;
    8.     public AudioSource click;
    9.  
    10.     public int buttonValue; // Assign the value for this button in the Inspector.
    11.     private NewKeypadController keypadController; // Assign the KeypadController reference in the Inspector.
    12.  
    13.     private void Start()
    14.     {
    15.         keypadController = GetComponentInParent<NewKeypadController>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.        
    21.  
    22.         if (Input.GetKeyDown(KeyCode.E))
    23.         {
    24.             Debug.Log("E key pressed.");
    25.  
    26.             Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
    27.             RaycastHit hit;
    28.  
    29.             if (Physics.Raycast(ray, out hit, raycastDistance, raycastLayerMask))
    30.             {
    31.                 if (hit.collider.CompareTag("Button"))
    32.                 {
    33.                     Debug.Log("Button pressed.");
    34.                     keypadController.AcceptInput(buttonValue);
    35.                     click.Play(); // Play a click sound
    36.                 }
    37.                 else
    38.                 {
    39.                     Debug.Log("Button not detected.");
    40.                     click.enabled = false;
    41.                 }
    42.             }
    43.         }
    44.         else
    45.         {
    46.            
    47.             click.enabled = false;
    48.         }
    49.     }
    50.  
    51.  
    52. }
    and this is the keypad controller script
    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.         dooranim = GetComponent<Animator>();
    66.         dooranim.SetBool("Opened", true);
    67.         Debug.Log("well done:)");
    68.         ResetCurrentCode();
    69.     }
    70.  
    71.  
    72.     void ResetCurrentCode()
    73.     {
    74.         currentIndex = 0;
    75.         currentCode = new int[code.Length];
    76.         for (int i = 0; i < currentCode.Length; i++)
    77.         {
    78.             currentCode[i] = -1;
    79.         }
    80.     }
    81.  
    82.     void Opendoor()
    83.     {
    84.         dooranim = GetComponent<Animator>();
    85.         opened = !opened;
    86.         dooranim.SetBool("Opened", !opened);
    87.         Debug.Log("well done:)");
    88.     }
    89. }


    thanks for your help in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Sounds like you wrote a bug!

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Add variables code and currentCode to the debug in your Fail() function. See what exactly gets compared.