Traps in Unity 2D Platform (Script)

Hello guys (this is my first post),

Basically I am pretty basic at programming, however I have managed to create a 2D platform game which works.
I now need to create traps where when the Player goes on top of a platform, spikes should appear restarting the level.

So the spikes isn’t there until the player touches the platform. Once the player touches it, it appears, killing the player.

How would I go about creating a script for this? I think know how to do the restart part of it…

Thank you.

Here is a link to a question about restarting level: Similar question

At the end of your “Die” function,
just add:

yield WaitForSeconds(5.0); // or
however long you want it to wait

For activating the spikes I would check out Triggers (Creating a collider above the platform and setting it to trigger in the editor). Than you`d have to write OnTriggerEnter2D() function for actions to execute when the player enters the trigger zone.

Hi it can be something basic like this.

public GameObject spikes; //This will create a slot in the editor in where you just drag and drop your spikes. At first this one has to be desactivated ( unmarked) next to its name 
    public GameObject player; // This creates a slot in the editor in where you just drag and drop your player. 

// Use this for initialization
void Start () 


// Update is called once per frame
void Update ()

void OnTriggerEnter2D(Collider2D other)   // The player enters a trigger collider (trap or spikes)
	if (other.gameObject.tag == "trap") 


	if (other.gameObject.tag == "spikes") 
		health = 0;
		player.SetActive(false); // Here you can make that if the health of the player goes to  0 it restarts, or if the player is not active it restarts, you can do a lot. Also, with animations you can play around .