Deactivate 1st checkpoint, after 2nd checkpoint is activated

Hi there

I’ve created 2 checkpoints in my level, and now I want to add a script, which tells the 1st checkpoint, that when the player hits the 2nd checkpoint, the 1st checkpoint should be deactivated. Which means, when the player goes back, and hits the 1st checkpoint again, after hitting the 2nd checkpoint, the player should still go back to the 2nd checkpoint.

(The flag on 1st checkpoint should still be up, even though it is deactivated)

Hope that makes since.

This is my code, so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CheckpointController : MonoBehaviour {
	
	public Sprite flagClosed;
	public Sprite flagOpen;
	
	private SpriteRenderer theSpriteRenderer;
	
	public bool checkpointActive;

	// Use this for initialization
	void Start () {
		theSpriteRenderer = GetComponent<SpriteRenderer>();
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	void OnTriggerEnter2D(Collider2D other)
	{
		if(other.tag == "Player")
		{
			theSpriteRenderer.sprite = flagOpen;
			checkpointActive = true;
		}
	}
}

I index my checkpoints with an integer, and I keep track of the max checkpoint index the player has trigger. If a player goes back and enters an earlier checkpoint area, I only trigger the checkpoint logic if the checkpoint’s index is less than the maximum checkpoint index the player has triggered.

Another approach would be just to disable the earlier checkpoint script/gameobject upon triggering the second checkpoint.

This seems a case where “emumerators” are usefull. Im not a profesional programer so if speak wrong let me know. But as far as i know, enumerators can store diferent “states” wich is better than using like boolean variables wich store only 2 values. The enumerator wich appear like “enum” in script. Can store only one active value from the ones you setup to be. For example

public enum DoorState { Closed = 0, Opening = 1, Opened = 2, Closing = 3, Stoped = 4}
public DoorState DoorBehave;

I use the enum for my door script state machine wich can be only in one state at the time and store several state that can be. In my case i added a five state wich is stopped, once that state is reach the door will never be opened or closed again. The code you should have in the case you use enums should be like.

public enum checkpointStates { inactive = 0, Active = 1, finished = 2}
public checkpointStates checkPStatus;

public void EnteringCheckPoint () {
switch(checkPStatus){
case checkpointStates.inactive :
//check if the checkpoint is reached and if so change the state machine.
print("im inactive");
break;
case checkpointStates.Active :
// if its active maybe you want this to call some events put them here.
print("im active");
break;
case checkpointStates.finished :
//Make your flag to appear like active or whatever and then just return.
print("i finished my job");
return;
break;
}
}

I have to say that this code is not finished and if you put this on an empty script specting to work it will not. Is just to show you how work the enum with switches. As i said before im not an expert just learning as anyone here. And anytime i need some script to perform some kind of only one state at the time i use this approach since is more clean that messing around with bunch of boolean values. To change the checkPStatus var, you have use something like “checkPStatus = checkpointStates.inactive”, you can use the index value to but i forget how it was done since i prefer to use the propper word key to acces the variable.