Reputation: 51
I'm beginner in Unity. I'm making a game in Unity 2D. In this game I have a player, platforms and a die_zone, but when my player die his sprite becomes invisible. How can I change my script to make the sprite visible?
Here is the script:
using UnityEngine;
public class Death : MonoBehaviour
{
public GameObject Respawn;
public Sprite Player_;
public GameObject Player;
public GameObject die_zone1;
public GameObject die_zone2;
public GameObject die_zone3;
public GameObject die_zone4;
public void die()
{
Player.transform.position = Respawn.transform.position;
Player.GetComponent<SpriteRenderer>().sprite = Player_;
}
void OnTriggerEnter2D(Collider2D deth)
{
if (die_zone1.tag == "Death" || die_zone2.tag == "Death" || die_zone3.tag == "Death" || die_zone4.tag == "Death")
{
die();
}
}
}
I will glad to any suggestion.
Thanks!
Upvotes: 2
Views: 320
Reputation: 7395
Let's break your code down to walk through its points of failure.
using UnityEngine;
public class Death : MonoBehaviour
{
public GameObject Respawn;
public Sprite Player_;
public GameObject Player;
public GameObject die_zone1;
public GameObject die_zone2;
public GameObject die_zone3;
public GameObject die_zone4;
Type/field declarations, probably not your problem
public void die()
{
Player.transform.position = Respawn.transform.position;
Player.GetComponent<SpriteRenderer>().sprite = Player_;
}
Suspect, as your bug happens when you die. But maybe there's other code executing?
void OnTriggerEnter2D(Collider2D deth)
{
if (die_zone1.tag == "Death" || die_zone2.tag == "Death" || die_zone3.tag == "Death" || die_zone4.tag == "Death")
{
die();
}
}
}
Ah, looks safe enough. There's a conditional invocation to die(). Presumably your code could crash in the conditional (diezonetag == ...)
, but that would abort further execution without having edited the Player object. So probably okay.
Alright, so that leaves us with die(). There are two lines, each is a point of failure. They seem to be independent changes of each other (move the player, change their sprite) so perhaps you can test each of them in isolation to determine your culprit. You could do this by commenting out one, running, and seeing if your game still breaks. If that works, then comment out the other (and uncomment the first), run, and hopefully your game breaks.
Player.transform.position = Respawn.transform.position;
Hm, so what is the value of Respawn.transform.position? If that's off-screen or invalid, one would suspect that'd make your player position offscreen or invalid.
You can debug this one by looking at your scene view before/after your Player has died. What's their position before / after?
Player.GetComponent<SpriteRenderer>().sprite = Player_;
What's Player_ set to? Is it, perhaps, null? Alternatively, if you changed your player's sprite to be its death-state sprite in-editor, do you see anything? Maybe your death sprite is fully transparent, or the combination of your death sprite and your spriterenderer's settings makes it not appear on your screen?
When you click on your player object, does the sprite field appear in the inspector, and is it set to something sane?
Actually, you're setting Sprite to a GameObject. How's that supposed to work? Are you setting Sprite to a GameObject containing your sprite, instead of a Sprite itself? This doesn't seem like it should compile. According to Unity's documentation (which I got to by googling unity spriterenderer sprite
) here https://docs.unity3d.com/ScriptReference/SpriteRenderer.html the type of SpriteRenderer.sprite is a Sprite, whereas you have a GameObject. Something seems wrong.
Upvotes: 1
Reputation: 2586
Remove this line, or check the sprite of Player_. Also double check if he is invisible or just moved back to the spawn point.
Player.GetComponent<SpriteRenderer>().sprite = Player_;
Upvotes: 0