- Home /
racing game multiple checkpoint system query
currently I have placed 2 checkpoints, halfcheckpoint and finalcheckpoint. at first finalcheckpoint is disabled this is script attached to halfcheckpoint
void OnTriggerEnter() { finalcheckpoint.SetActive(true); halfcheckpoint.SetActive(false); }
so when player reaches halfcheckpoint, finalcheckpoint is enabled and halfcheckpoint is disabled
this is for finalcheckpoint
void OnTriggerEnter() { finalcheckpoint.SetActive(false); halfcheckpoint.SetActive(true); }
so when player reaches finalcheckpoint, finalcheckpoint is disabled and halfcheckpoint is enabled again. this works perfect.
but the problem is I want to have more than 2 checkpoints like 8 or 10 or something. so I will have to write separate script for all checkpoint1, checkpoint2, etc and attach it to respective checkpoint. I dont want to do that. is there a way I can write 1 script and attach it to parent gameobject of all the children checkpoints ??????
Answer by $$anonymous$$ · Jul 14, 2018 at 10:52 AM
Add script to your player that detects checkpoints.
GameObject lastCheckpoint;
void OnTriggerEnter(Collider other ) {
if (other.gameObject.tag == "checkpoint") {
if (lastCheckpoint) {
lastCheckpoint.setActive(false);
}
other.gameObject.setActive(true);
lastCheckpoint = other.gameObject;
}
Add the tag to your checkpoints.
I dont understand. I'm new to progra$$anonymous$$g. explaining each and every step will be very helpful. thank you
what I want to do is create empty(CheckPoints), make all checkpoints its child. attach script to this empty as follows
public class CheckPoints : $$anonymous$$onoBehaviour {
public GameObject CheckPoint1;
public GameObject CheckPoint2;
public GameObject CheckPoint3;
public GameObject CheckPoint4;
void OnTriggerEnter(*CheckPoint2*)
{
CheckPoint3.SetActive(true);
CheckPoint2.SetActive(false);
}
void OnTriggerEnter(*CheckPoint3*)
{
CheckPoint4.SetActive(true);
CheckPoint3.SetActive(false);
}
}
and so on how do I make it work
Yeah sure. So do you just want to set them active and not active just for visual effects, or do you need to record the checkpoints that have been passed through?
Your answer
![](https://koobas.hobune.stream/wayback/20220612170702im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Xamarin opens multiple windows. 0 Answers
Add a different game to another 1 Answer
How to save multiple instances of the same class 3 Answers
Play back multi channel audio 0 Answers
Checkpoints? 0 Answers