Search Unity

Enabling a SpriteRenderer?

Discussion in '2D' started by jhervas, Jan 20, 2019.

  1. jhervas

    jhervas

    Joined:
    Jan 18, 2019
    Posts:
    1
    Hi, I'm learning Unity, and i've run into a problem when trying to enable one SpriteRenderer from a C# script.

    I have one static object on my game with a rigidbody 2d and a box collider. When the player collides with this object I want to enable one UI element (a text board). The code I'm using is the next:

    Code (CSharp):
    1. public class ObjectController : MonoBehaviour
    2. {
    3.     public SpriteRenderer UI;
    4.  
    5.     void OnCollisionEnter2D(Collision2D collision)
    6.     {
    7.         Debug.Log("hello");
    8.         UI.enabled = true;
    9.     }
    10. }
    Then I've used the Inspector to drag and drop the SpriteRenderer element to the public variable of the script. When the player collides with the Object, I see on the console the logs "hello", but the SpriteRenderer dosn't change at all.

    Any idea of what I could be doing wrong?

    Thanks in advance guys,
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    UI.SetActive(true);