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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Click on gameObject with disabled renderer?

Discussion in 'Scripting' started by DrBibop, Nov 20, 2015.

  1. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    I'm trying to make my 2D object toggle visibility when I click on it. To make it invisible, I turn off the renderer. The problem is that when the renderer is disabled, It doesn't detect my clicks so it cannot turn back to visible. Here's the script for now:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class Drumnote : MonoBehaviour {
    6.  
    7.     void Awake ()
    8.     {
    9.         gameObject.GetComponent<Renderer>().enabled = false;
    10.     }
    11.    
    12.     void Update ()
    13.     {
    14.  
    15.     }
    16.     void OnMouseDown()
    17.     {
    18.         if (gameObject.GetComponent<Renderer>().enabled == false)
    19.         {
    20.             gameObject.GetComponent<Renderer>().enabled = true;
    21.         }
    22.         if (gameObject.GetComponent<Renderer>().enabled == true)
    23.         {
    24.             gameObject.GetComponent<Renderer>().enabled = false;
    25.         }
    26.     }
    27. }
    28.  
    Any idea?
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    your code working:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ToggleVisible : MonoBehaviour {
    5.  
    6.     Renderer renderer;
    7.     bool isVisible;
    8.  
    9.     void Awake ()
    10.     {
    11.         renderer = GetComponent<Renderer>();
    12.     }
    13.  
    14.     void OnMouseDown()
    15.     {
    16.         isVisible = !isVisible;
    17.         renderer.enabled = isVisible;
    18.     }
    19. }
    20.  
    but you say 2D objects so maybe better:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(Collider2D))]
    5. public class ToggleVisible : MonoBehaviour {
    6.  
    7.     SpriteRenderer renderer;
    8.     bool isVisible;
    9.  
    10.     void Awake ()
    11.     {
    12.         renderer = GetComponent<SpriteRenderer>();
    13.     }
    14.  
    15.     void OnMouseDown()
    16.     {
    17.         isVisible = !isVisible;
    18.         renderer.enabled = isVisible;
    19.     }
    20. }
    21.  
     
    DrBibop likes this.
  3. DrBibop

    DrBibop

    Joined:
    Oct 9, 2015
    Posts:
    18
    THANK YOU! It worked. Just had to turn off the renderer on awake on your script and everything works fine! Thank you again!
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Renderer is the base class for SpriteRenderer and MeshRenderer, so the original code works for both situation. Also, your 'isVisible' bool is redundant:

    Code (CSharp):
    1. [RequireComponent (typeof(Collider2D))]
    2. public class ToggleVisible : MonoBehaviour {
    3.  
    4.     SpriteRenderer renderer;
    5.  
    6.     void Awake ()
    7.     {
    8.         renderer = GetComponent<SpriteRenderer>();
    9.     }
    10.  
    11.     void OnMouseDown()
    12.     {
    13.         renderer.enabled = !renderer.enabled;
    14.     }
    15. }
     
    jister likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Its probably worth knowing why too.

    OnMouseDown has nothing to do with the renders, it works on colliders.

    The original script was checking if the renderer was enabled after you had just enabled it. Meaning the second if would always run. Making it an else if would also have worked.