1
\$\begingroup\$

I have some trouble finding out how I can load my character at the start of the level, instead of resetting the whole thing. The game mechanics are quite straight forward; don't touch the walls. If the player touches the walls, he "pops" (seeing as it is a balloon.)

This is my current script:

public class playerSpawner : MonoBehaviour {

public Sprite charAlive, charDead;
public AudioClip popAudio;

void  OnCollisionEnter2D (Collision2D col ){

    if (col.gameObject.name == "dirtWalls") {
        AudioSource.PlayClipAtPoint(popAudio, transform.position);
        gameObject.GetComponent<SpriteRenderer>().sprite = charDead;
        StartCoroutine (Dead ());
    }
    if (col.gameObject.name == "gooWalls") {
        gameObject.GetComponent<SpriteRenderer>().sprite = charDead;
        StartCoroutine (Dead ());
    }
    if (col.gameObject.name == "grassWalls") {
        gameObject.GetComponent<SpriteRenderer>().sprite = charDead;
        StartCoroutine (Dead ());
    }
}

IEnumerator Dead() {

    AudioSource.PlayClipAtPoint(popAudio, transform.position); // PLAYS SEVERAL TIMES
    yield return new WaitForSeconds(3/2);
    Application.LoadLevel ("untitled");
    // LOAD LEVEL RESETS EVERYTHING, MAKE LOAD CHAR INSTEAD?
}

}

How can I just respawn the player object instead of resetting the whole scene?

\$\endgroup\$
2
  • \$\begingroup\$ Not sure if I understand your question but look up the Resources.Load() function to load an object through script... As for optimizing the collision code, if it works and isn't causing any frame rate issues then don't try changing it... If later on it causes issues then look into altering it. \$\endgroup\$
    – Savlon
    Commented Nov 15, 2014 at 13:36
  • \$\begingroup\$ Basically what I want to do is destroy the player when he hits the wall. I have a coroutine that waits a sec before loading the level. Loading the level means the music also resets to it's start. \$\endgroup\$
    – Blobbey
    Commented Nov 15, 2014 at 14:11

2 Answers 2

1
\$\begingroup\$

You could turn your player into a prefab. When your player "pops" destroy that gameobject and create a new one at the start of the level.

\$\endgroup\$
0
\$\begingroup\$

As ThePumkinMelon suggested, create a prefab for your player game object.

Then remove the player object from your scene, have your player spawner reference the player prefab and also reference the current player game object in the scene:

public class PlayerSpawner : MonoBehaviour {

    public GameObject playerPrefab;
    private GameObject currentPlayer;

Then in the Update-method of the PlayerSpawner, do this:

    if (currentPlayer == null) {
         currentPlayer = Instantiate(playerPrefab, transform.position, transform.rotation);
    }

This will check if its player is in the scene, and when it is not it will spawn a new player at the position and with the rotation of the spawner (that means you need to place the PlayerSpawner game object at the location where you want the player to spawn).

There are two cases where currentPlayer == null will be true.

Now your Dead coroutine just needs to Destroy the player game object and the PlayerSpawner will promptly instantiate a new one.


However, keep in mind that if you excessively destroy and instantiate a lot of game objects, you might start to notice notable performance degregation due to memory fragmentation and garbage collection. In this case this shouldn't matter, because the player will die every few seconds at most, even if you create a hellishly difficult game. But if you have some kinds of objects in your game which get destroyed and respawned serveral times per second, then you might want to look into a pattern called "object pools". But that's a topic for a different question.

I am looking forward to playing your game.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .