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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Cannot disable collider

Discussion in 'Getting Started' started by linglonglinglong, Apr 9, 2018.

  1. linglonglinglong

    linglonglinglong

    Joined:
    Feb 26, 2018
    Posts:
    20
    I want to disable the collider of the object when I hide it. But now when it disappears, I go to the spot of the object and the event happens when I touches air! Should I just
    collider.enable = false
    or should I add more things?

    Here's my code just for reference:
    Code (CSharp):
    1. public string type;
    2.     public bool avail;
    3.     public float time;
    4.     public float appearTime;
    5.     public float disappearTime;
    6.  
    7.     private Collider coll;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         coll = this.GetComponent<Collider> ();
    12.         time = Random.Range (1, 5);
    13.         if (time < 2) {
    14.             avail = false;
    15.         } else {
    16.             avail = true;
    17.         }
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         time -= Time.deltaTime;
    23.         if (time < 0f) {
    24.             ShowAndHide ();
    25.         }
    26.     }
    27.        
    28.     void ShowAndHide(){
    29.         if (avail) {
    30.             time = disappearTime;
    31.             avail = false;
    32.             this.gameObject.GetComponent<Renderer> ().enabled = false;
    33.             coll.enabled = false;
    34.         }
    35.         else {
    36.             time = appearTime;
    37.             avail = true;
    38.             this.gameObject.GetComponent<Renderer> ().enabled = true;
    39.             coll.enabled = true;
    40.         }
     
  2. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Ok, I'm not going to answer your question but I will offer an alternative for you to try.

    In stead of trying to disable the Renderer component and the collider, use this:

    Code (CSharp):
    1. this.gameObject.setActive(true);
    2.  
    3. this.gameObject.setActive(false);
    I think this will do what you're trying to do.
     
  3. linglonglinglong

    linglonglinglong

    Joined:
    Feb 26, 2018
    Posts:
    20
    I tried this. But this would stop the timer. What I want is to let the timer continue to run when the object disappears.
     
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Try moving your timer to your game manage or other script so it's not disabled with the game object.
     
  5. linglonglinglong

    linglonglinglong

    Joined:
    Feb 26, 2018
    Posts:
    20
    Oh! Ok I get what you mean. Thank you!