- Home /
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);
}
}
You need GetComponent() to access the variable. I've never tried it with a class, so I suppose it will be a little more tricky. by the way, try GameObject.FindWithTag("Player").GetComponent(PlayerObject).currentCheckpoint = currentCheckpoint + 1; ins$$anonymous$$d that PlayerObject.currentCheckpoint = (currentCheckpoint + 1); in the first script. I hope that my syntax is correct, but it should point you in the right direction.
Thanks for the contribution BiG.
Unfortunately both your good attempt line and the line "if (currentCheckpoint = PrevCheckNo)" have contextual errors (variables not existing in the context)in addition with 'The best overloaded method match for `UnityEngine.GameObject.GetComponent(System.Type)' has some invalid arguments'.
At least the PlayerObject class has no errors, with the void GUI part as a comment as I am leaving that for later.
We're in some sort of trap. But I appreciate your guys helping me get out of it :D
Answer by SimonDenton · Jan 03, 2012 at 04:35 AM
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++;
}
Answer by ks13 · Dec 16, 2011 at 09:58 AM
Hi, try removing "static" between "private" and "int", for currentLap and currentCheckpoint.
Thank you for your input ks13. I still have errors though as listed below:
Assets/Scripts/RaceCheckPoint.cs: error CS1061: Type UnityEngine.GameObject' does not contain a definition for
currentCheckpoint' and no extension method currentCheckpoint' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
Assets/Scripts/RaceCheckPoint.cs: error CS0103: The name `currentCheckpoint' does not exist in the current context
That's because you made it private...put it public so other classes will be able to acess them.
if (currentCheckpoint = PrevCheckNo)
{
GameObject PlayerObject = GameObject.FindWithTag("Player");
PlayerObject.currentCheckpoint = (currentCheckpoint + 1);
}
Shouldn't this be
GameObject PlayerObject = GameObject.FindWithTag("Player");
if (PlayerObject.currentCheckpoint = PrevCheckNo)
{
PlayerObject.currentCheckpoint = (PlayerObject.currentCheckpoint + 1);
}
I mean, where do you create/get your currentCheckpoint variable? the only declaration i see is in PlayerObject, not in RaceCheckPoint.
Yes I declare currentCheckpoint in the PlayerObject script and the RaceCheckPoint is meant to change that property during the OnTriggerEnter(changed from on OnCollisionEnter).
Thanks for your logical lines! Apart from UnityEngine.GameObject' does not contain a definition for
currentCheckpoint' and no extension method currentCheckpoint' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?) it is good to go!
Answer by ks13 · Dec 22, 2011 at 02:39 PM
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.
Sorry about the confusion. Checkpoint is the name of an object that is a collider trigger. RaceCheckPointScript is the script attached to it. The checkpoint has only one variable; prevCheckNo. The player script has a variable currentCheckpoint. If the integer value of currentCheckpoint is equal to the integer value of variable prevCheckNo, then currentCheckpoint gets +1. So:
//Update checkpoint number
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.tag == "Checkpoint"){
RaceCheckPointScript RaceCheckpoint;
RaceCheckpoint = gameObject.GetComponent("RaceCheckPointScript") as RaceCheckPointScript;
if (currentCheckpoint == RaceCheckpoint.prevCheckNo)
{
currentCheckpoint = currentCheckpoint +1;
}
}
}
That seems correct but, that is a code applicable on PlayerObject, not CheckPoint. I thought the code was to be put on CheckPoint, but that's up to you to decide.
Yeah I decided for PlayerObject to do all the work and let the checkpoint just have its variable integer and nothing more. And you know what? I got NO ERRORS! Problem? currentCheckpoint doesn't change. However, the laps and so forth work beautifully!
Have you checked the begining value of currentCheckpoint and the value of the first PrevCheckNo?
Yes in the player code currentCheckpoint is 0 and on the first Checkpoint object the PrevCheckNo is 0 as well. So when we collide with this one, since they are both equal, the +1 operation should work so that currentCheckpoint is 1. Then when we touch the next checkpoint object (PrevCheckNo = 1) it adds again etc.
Your answer
Follow this Question
Related Questions
Setting up player positions in a racing game 1 Answer
How Do I Set The Number Of Laps? 1 Answer
How do I make laps in a racing game? 5 Answers
Motorcycle Controller for Mobile 0 Answers