- Home /
Make 2d sprites invisible?
I'm trying to make most of the sprites in my game invisible. With the exception of a few that are tagged "Player", "Background" "Ground".
I tried this
if(!gameObject.CompareTag("Player"))
{
gameObject.renderer.enabled= false;
}
But I get an error about the renderer missing.
You have to get way more detailed: Where is this code in? And if all Sprites have this code attached you have to do this:
if(gameObject.tag != "Player" || gameObject.tag != "Ground" || ...) {
gameObject.renderer.enabled = false;
}
If 'gameObject' is a sprite, then your code should work. Any chance it is an empty parent or child object? Insert this just before the 'enabled = false' line:
Debug.Log(gameObject.name + ", " + gameObject.tag + ", " + gameObject.renderer);
Answer by Vitor_r · May 12, 2014 at 04:22 PM
Maybe this?
if(!gameObject.CompareTag("Player"))
{
if(gameObject.renderer != null)
{
gameObject.renderer.enabled= false;
}
}
Answer by JayOhhh · May 30, 2014 at 04:20 PM
You need to get the component of the gameObject and then turn the SpriteRenderer off.
public SpriteRenderer sprender;
void turnOff() {
if(!gameObject.CompareTag("Player"))
{
sprender = gameObject.GetComponent<SpriteRenderer>();
sprender.enabled= false;
}
}
You don't; SpriteRenderer inherits from Renderer. Also don't make public variables unnecessarily like that.
You don't have to, but with the limited information this is a way it would work. I wouldn't make the public variables unless needed. This was just an example..
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Object Changing Y Coordinate in Game View 0 Answers
Button Enabling/Disabling using Collision Triggers? 1 Answer