Making a sprite invisible/visible in script (C#)

Updated on July 30, 2014 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
7 on July 24, 2014

I’m trying to figure out how to make a sprite above my player character appear and disappear when I need it to in C# script. I tried looking up the functions used with SpriteRenderer but none of them seemed to be what I needed. I though I could use the variable isVisible but it’s read-only.

  • Liked by
Reply
0 on July 24, 2014

what about deactivating the whole object?

  • Liked by
Reply
Cancel
0 on July 24, 2014

Is that the same thing, or does it have other consequences? Also, what function would I use to do that in a script?

  • Liked by
Reply
Cancel
0 on July 24, 2014

To turn On and Off a GameObject: 

First you need to set a variable to tell unity which GameObject are you going to appear/Disappear, then you use this: 

GameObject.SetActive(true/false);

  • Liked by
Reply
Cancel
0 on July 24, 2014

I got it to work. Thanks!

  • Liked by
Reply
Cancel
1 on July 29, 2014

The consequences of disabling a gameObject is that it component on it won’t work on it in the game until you make it active again.

If you want the gameObject to still be active and it script and comonent to still work while it invisble ,just turn invisible, in your class put this code
Easy way

public/private SpriteRenderer sr;
public/private bool visible=true/false;
void Start(){
sr=gameObject.GetComponent();
}
void Update(){
if(visible==true){
sr.enabled=true;
}
if(visible==false){
sr.enabled=false;
}
}

Hard way(if you still want to use the sprite renderer in a way);

public/private SpriteRenderer sr;
public/private bool visible=true/false;
void Start(){
sr=gameObject.GetComponent();
}
void Update(){
if(visible==true){
sr.material.color=new Color(sr.material.color.r,sr.material.color.g,sr.material.color.b,255);
}
if(visible==false){
sr.material.color=new Color(sr.material.color.r,sr.material.color.g,sr.material.color.b,0);
}
}

This only check if it visible and set the sprite to be visible or invisible, you would have to make changing the visible variable part depend on how it will work
Hope this help even if you don’t need it.

Guru
on July 30, 2014

Hi Luigi 🙂  I’m afraid that your script won’t work properly, it will check every frame too see if the “var visible” is true or false and it would proceed to change it. You will end up with a nice loop hehe. 
Although is true that disabling a GameObject will disable its components too, you shouldn’t put important components on a item that will disappear and reappear.

Show more replies
  • Liked by
Reply
Cancel
0 on July 29, 2014

alternate way to MegaLuigi’s first one

SpriteRenderer theSprite;
void Start()
{
 theSprite = gameobject.GetComponent<SpriteRenderer>();
}
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
if(theSprite.ActiveSelf)
{
theSprite.SetActive(false);
}
else
{
theSprite.SetActive(true);
}
}
}

if you left click, it’ll toggle sprite visibility

  • Liked by
Reply
Cancel