You are on page 1of 2

Lesson 10 Transport to a new Location

Before making the code for transferring over to a new stage, there are some changes for the player
score script:
void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.tag == "endlevel")
{
CountScore();
timeLeft = 120;
}
if (trig.gameObject.tag == "coin")
{
playerScore += 10;
Destroy(trig.gameObject);
}

As soon as they hit the endlevel the time will be set back to 120 and score will be
counted.

Prepare two images to be used as spawn point. Add box collider on them and be sure to
tick the isTrigger.

spA

sp1 = spA

sp2 = spB

spB

sp1 = spB

sp2 = spA

Complete code below using System.Collections;


using System.Collections.Generic;
using UnityEngine;

public class GoToNewArea : MonoBehaviour {

public GameObject sp1, sp2; //spawnpoint 1 and 2


public static bool transport;

private void Start()


{
transport = true;
sp1 = this.gameObject;
}
private void OnTriggerEnter2D(Collider2D trig)
{
if (transport == true)
{
trig.gameObject.transform.position = sp2.gameObject.transform.position;
transport = false;
}
}

private void OnTriggerExit2D(Collider2D trig)


{
transport = true;
}
}

You might also like