- Home /
OnCollisionEnter Won't Edit Other Variables
Alright, so I have created a little racing game. I was making a checkpoint, so that you couldn't cross the finish line and get a lap without passing the checkpoint. At first I just wanted it to be an OnTriggerEnter, which worked fine. But in 2 player, that wouldn't work. I have 3 scripts (1 for the finish line, 1 for the checkpoint for P1, and 1 for the checkpoint for P2), and when I pass the checkpoint, it won't edit the variables! Here's the script for the checkpoint for P1:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Checkpoint : MonoBehaviour {
public Text Checker;
public Text Reseter;
private int checkpoint;
private int checkreset;
// Use this for initialization
void Start () {
Checker.text = "0";
checkpoint = 0;
}
// Update is called once per frame
void Update () {
if(Reseter.text == "1"){
checkpoint = 0;
}
if(checkpoint == 1){
Checker.text = "1";
}
}
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Player 1"){
checkpoint = 1;
Reseter.text = "0";
}
}
}
And for P2:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Checkpoint2 : MonoBehaviour {
public Text Checker2;
public Text Reseter2;
private int checkpoint;
private int checkreset;
// Use this for initialization
void Start () {
Checker2.text = "0";
checkpoint = 0;
}
// Update is called once per frame
void Update () {
if(Reseter2.text == "1"){
checkpoint = 0;
}
if(checkpoint == 1){
Checker2.text = "1";
}
}
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Player 2"){
checkpoint = 1;
Reseter2.text = "0";
}
}
}
Here's the script for the Finish Line:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Lap : MonoBehaviour {
public Text MyText;
public Text MyText2;
public Text Checker;
public Text Checker2;
public Text Reseter;
public Text Reseter2;
private int score;
private int score2;
private int counter;
private int counter2;
// Use this for initialization
void Start () {
score = 0;
score2 = 0;
counter = 1;
counter2 = 1;
Reseter.text = "0";
Reseter2.text = "0";
}
// Update is called once per frame
void Update () {
MyText.text = "Lap: "+ counter;
MyText2.text = "Lap: "+ counter2;
if(counter >= 3){
MyText.text = "You beat the race!";
if(Input.GetMouseButtonDown(0)){
Application.LoadLevel("Menu");
}
}
if(counter2 >= 3){
MyText2.text = "You beat the race!";
if(Input.GetMouseButtonDown(0)){
Application.LoadLevel("Menu");
}
}
}
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "P1"){
if(Checker.text == "1"){
score = score + 1;
counter = score + 1;
Checker.text = "0";
Reseter.text = "1";
}
}
if(col.gameObject.name == "P2"){
if(Checker2.text == "1"){
score2 = score2 + 1;
counter2 = score2 + 1;
Checker2.text = "0";
Reseter2.text = "1";
}
}
}
}
Is there a mistake I made that doesn't show up in the editor? Thanks in advance.
Your answer
Follow this Question
Related Questions
Trying to get collision detection between a game object and signed distance field [SDF] 0 Answers
if statement not working when detecting collision between two prefabs 1 Answer
Collision 2D doesnt work 0 Answers
how do i create scripts for creating question and answer system? 0 Answers
Why my capsule collider sometimes stucks in other colliders? 0 Answers