• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Sugar Mommy · Jun 17, 2015 at 10:45 PM · 2dspriteplatformerspriterenderersorting layers

[2D] [C#] Sprite disappears after changing position

I've run into a problem that's been bothering me a little bit. I'm sure the cause of it is something very simple, but I can't seem to think of anything.

My scene currently has a set of spikes and a player. Whenever the player collides with the spikes, it's supposed to respawn at a checkpoint. I've used the following scripts for this:


KillPlayer (attached to Spikes)

 public LevelManager levelManager;
 
     void Start () 
     {
         levelManager = FindObjectOfType<LevelManager>();
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.name == "player")
         {
             levelManager.RespawnPlayer();
         }
     }



LevelManager (attached to empty gameobject in scene)

  public GameObject currentCheckpoint;
     private PlayerController player;
 
     void Start () 
     {
         player = FindObjectOfType<PlayerController>();
     }
 
     public void RespawnPlayer()
     {
         Debug.Log("Player respawned.");
         player.transform.position = currentCheckpoint.transform.position;
     }



I know this script works, because when I move the player into the spikes, the camera moves back to the position of the checkpoint. However, after this happens, I can't see my player anymore.

After some research I found that the problem might have something to do with the Sprite Renderer's Sorting Layer being set on default, but I'm too much of a newbie to know what I'm supposed to change to fix this.

To be sure, here's a few screenshots.

alt text alt text

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hickna · Jun 18, 2015 at 02:13 AM 0
Share

Probably is a simple issue with axis or rotation when you respawn the player. I didn't understand you question exactly, but for sure you need do keep your player "alive", just enable and disable, changing the position to the previous checkpoint. $$anonymous$$aybe you can create a Singleton in this case if you are changing the scene too.

avatar image digzou · Jun 18, 2015 at 09:36 AM 0
Share

Check the 'Z' of the transform o your camera and 'disappearing_gameObject' before and after doing the your update in game. The case should be that the z value of either of those gameobjects is changing.

avatar image Sugar Mommy · Jun 18, 2015 at 01:15 PM 0
Share

Yep, looks like it was the Z-axis being wrong after all. Thank you very much for your answers!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by komodor · Jun 18, 2015 at 10:09 AM

 player.transform.position = new Vector3(currentCheckpoint.transform.position.x, currentCheckpoint.transform.position.y, player.transform.position.z);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disabled sprite renderer's briefly show on level load 2 Answers

My sprite does not change 1 Answer

Blood splats 2d that sticks only on one 2d sorting layer 0 Answers

Pixels from a sprite aren't the same size 0 Answers

How to truncate sprite without changing the scale of the sprite? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges