Check my race code please

There seem to be problems with accessing the PlayerObject data as well as displaying the number of current checkpoints and laps.

Code for the game object ‘RaceCheckpoint’, touch this to make a checkpoint progression

public int PrevCheckNo =0; //So laps have to be obtained in order e.g the previous checkpoint has to be 1 in order to do + 1 and make this prev checkpoint a 2
	
	void Awake ()
	{
		
	}
	void OnCollisionEnter() {
	
		if (currentCheckpoint = PrevCheckNo)
		{
		GameObject PlayerObject = GameObject.FindWithTag("Player");
		PlayerObject.currentCheckpoint = (currentCheckpoint + 1);
		}
	}

Code for class ‘PlayerObject’, which is the player with all components

private static  int currentCheckpoint = 0; //Current checkpoint
private static  int currentLap = 0; //Current lap

public int LapTotal = 0; //How many laps in the race overall?
public int CheckpointTotal =0; // How many checkpoints in this race?

public string CurrentCheckText = "Current Checkpoint";
public string CurrentLapText = "Current Lap";

void Update ()
{

// Race Checkpoint System
if (currentCheckpoint > CheckpointTotal)
		{
			currentLap = (currentLap + 1); //Add a lap
			currentCheckpoint =0; //Reset checkpoint total
		}
if (currentLap > LapTotal)
		{
			//End of our turn in this race
			
		}
		
		}
void OnGUI() {
        CurrentCheckText = GUI.TextField(new Rect(10, 10, 200, 20), CurrentCheckText: currentCheckpoint, 25);
		
		
        CurrentLapText = GUI.TextField(new Rect(10, 50, 200, 20), CurrentLapText: currentLap, 25);
    }
    }

Hi, try removing “static” between “private” and “int”, for currentLap and currentCheckpoint.

Ok, you're actually just confusing me more, and your code of RaceCheckPoint is weird :

If you read this you’ll see that OnCollisionEnter actually takes a parameter, which is passed by unity and not your scripts. that way you can get the transform of the collider that triggered the event. So you can use a code like :

void OnCollisionEnter(Collision collision) {
   scriptName = col.transform.GetComponent("scriptName");
   if (scriptName.currentCheckpoint == PrevCheckNo) //Variables names usually start with lower case characters, and Methods/Functions with upper, that way it's easier to distinguish them
      {
        scriptName.currentCheckpoint += 1;
      }
   else
   DisplaySomeErrorMessage();
}

Don't forget to use the script's name for the search, because when i read carefully i think your problem is you're trying to use a Transform as a script, which is wrong and why it dsplays those errors.

I managed to solve this issue with the help of a friend. Here is the working player code:

void OnTriggerEnter(Collider collision)
{
		//Update checkpoint number

	
        RaceCheckPointScript Checkpoint;
		print(collision.gameObject.tag);
        Checkpoint = collision.gameObject.GetComponent<RaceCheckPointScript>();
		if (Checkpoint)
	        if (currentCheckpoint == Checkpoint.prevCheckNo)
			{
	                Debug.Log("Checking if checkpoint is prev");
	                currentCheckpoint++;
			}