Search Unity

Question Trouble with OnTriggerEnter: Script won't trigger/beginner

Discussion in 'Physics' started by Mothman_exe, Jun 3, 2023.

  1. Mothman_exe

    Mothman_exe

    Joined:
    Jun 3, 2023
    Posts:
    2
    Hi beginner working on a school assignment here!!
    Am trying to set up an object that when the player runs into it shows a little "press E to interact" and then does/does not do a bunch of things depending on whether on not an interact bool is set to true.

    Its working and telling me when player collides with object but doesn't trigger the rest of the script when I press E/the interact button which I've set up in the input manager

    If anyone could read through the code to see where I've messed up would be a lifesaver!!
    Any help would be appreciated as I'm on a time limit!

    Code (CSharp):
    1. void Start()
    2.     {
    3.  
    4.         InteractPrompt.SetActive(false);
    5.         WaitPrompt.SetActive(false);
    6.  
    7.  
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (InteractOn == true)
    14.         {
    15.             objectToAnimate.SetBool(openBooleanName, true);
    16.         }
    17.         else
    18.         {
    19.             objectToAnimate.SetBool(openBooleanName, false);
    20.         }
    21.          
    22.  
    23.  
    24.     }
    25.     private void OnTriggerEnter(Collider other)
    26.     {
    27.         Debug.Log("Collide working");
    28.         if (InteractOn == true & other.gameObject.CompareTag("player"))
    29.         {
    30.             InteractPrompt.SetActive(true);//E to interact
    31.          
    32.             Debug.Log("Interact on");
    33.  
    34.         }
    35.         if (Input.GetButtonDown("Interact") & InteractOn == true & other.gameObject.CompareTag("player"))
    36.         {
    37.             StartCoroutine(yabbyWait(YabbyTimer));
    38.             InteractOn = false;
    39.             //Stick a particle system hear and some sound
    40.             Debug.Log("Interact off/button pressed");
    41.         }
    42.        
    43.         if (other.gameObject.CompareTag("player") & InteractOn == false) ///(InteractOn == false)
    44.         {
    45.             //Insert dialogue system
    46.             WaitPrompt.SetActive(true);
    47.             objectToAnimate.SetBool(openBooleanName, false);
    48.             Debug.Log("Wait");
    49.  
    50.         }
    51.         if(MoveToNext == true & other.gameObject.CompareTag("player"))
    52.         {
    53.             InteractPrompt.SetActive(true);
    54.  
    55.             if(Input.GetButtonDown("Interact"))
    56.             {
    57.                 Cam2.SetActive(true);
    58.                 Cam2.SetActive(false);
    59.                 Cam3.SetActive(false);
    60.                 Cam4.SetActive(false);
    61.             }
    62.          
    63.            
    64.         }
    65.      
    66.     }
    67.     IEnumerator yabbyWait(float time)
    68.     {
    69.         yield return new WaitForSeconds(time);
    70.         objectToAnimate.SetBool(openBooleanName, true);
    71.         MoveToNext = true;
    72.         Debug.Log("Yabby Game Open!!");
    73.     }
    74.  
    75. }
    76.  
     
    Last edited: Jun 3, 2023