Search Unity

Made An Isinsidecollider Bool, But It Doesn't Want To Work

Discussion in 'Scripting' started by OlafsOsh, Apr 10, 2019.

  1. OlafsOsh

    OlafsOsh

    Joined:
    Jul 10, 2018
    Posts:
    61
    Hey!

    So, I wrote this and even did not test it right away, so certain I was that this one is correct... and was wrong.
    The idea - once inside the collider this script is attached to, pressing "Space" will activate the PauseMenu.

    But it doesn't. What went wrong? Do you see?
    The collider triggers also emission of the particle system, that one works fine.
    P.S. Usually throughout the level PauseMenu is SetActive by Esc key.

    fb28.PNG
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine; using UnityEngine.SceneManagement;
    4.  
    5. public class ColliderToTriggerParticlesCustom : MonoBehaviour
    6. {
    7.     [SerializeField] private ParticleSystem _promptParticles; //edited GameObject into Particle System
    8.     private ParticleSystem.EmissionModule emissionModule; //edit_2
    9.     public AudioClip audioClip;
    10.     AudioSource audioSource;
    11.     bool isInsideCollider;
    12.     public GameObject pauseMenuUI;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.  
    18.         emissionModule = _promptParticles.emission;
    19.         emissionModule.enabled = false;
    20.  
    21.         audioSource = GetComponent<AudioSource>(); //Might consider to put this in separate Awake method
    22.     }
    23.     void OnTriggerEnter2D(Collider2D collider)
    24.     {
    25.         isInsideCollider = true;
    26.         print("Player colliding with " + tag);
    27.         switch (collider.tag)
    28.         {
    29.             case "Player":
    30.                 emissionModule.enabled = true;
    31.                 audioSource.PlayOneShot(audioClip, 1F);
    32.                 break;
    33.         }
    34.  
    35.         if (isInsideCollider == true && Input.GetKeyDown(KeyCode.Space))
    36.         {
    37.             pauseMenuUI.SetActive(true);
    38.         }
    39.  
    40.     }
    41.     void OnTriggerExit2D(Collider2D collider)
    42.     {
    43.         isInsideCollider = false;
    44.         emissionModule.enabled = false;
    45.         audioSource.Stop();
    46.     }
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.  
    51.     }
    52. }
     
  2. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    I would try to move :

    Code (CSharp):
    1. if (isInsideCollider == true && Input.GetKeyDown(KeyCode.Space))
    2. {
    3.     pauseMenuUI.SetActive(true);
    4. }
    to your Update function.
     
  3. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    • OnTriggerEnter is only called a single time when something enters the trigger, so you don't want to check for input inside of it.

    Instead, override OnTriggerStay and test for input there, or start a coroutine that checks for input. Or, if you have a input manager on the player object, use get component to toggle a bool on the input manager as inside trigger.