# Positioning script

Hey,

I have made a script that should work but it doesn’t. I tried to figure out it for hours now but I couldn’t find any mistake.

The script is supposed to find out the players position in a race based on points I calculated earlier. But I get the same position for every player

so this is the script:

``````static var currentPosition : carsPosition [] = new carsPosition [6];

static class carsPosition
{
var currentPosition : int;
var points : float;
}

InvokeRepeating("CalculatePosition",2,2);

function Start()
{
for(var c : int; c < currentPosition.length; c++)
{
currentPosition[c].currentPosition = c;
currentPosition[c].points = c;
}
}

function CalculatePosition()
{
for(var i : int; i < currentPosition.length; i++)
{
for(var b : int; b < currentPosition.length; b++)
{
if(i != b)
{
if(currentPosition[i].currentPosition <= currentPosition[b].currentPosition)
{
print("check ");
if(currentPosition[i].points <= currentPosition[b].points)
{
print("More Points ints i = " + i + "and b = " + b);
print("punkte");
currentPosition[i].currentPosition = currentPosition[i].currentPosition + 1;
currentPosition[b].currentPosition = currentPosition[b].currentPosition - 1;
}
}
if(currentPosition[i].currentPosition == currentPosition[b].currentPosition)
{
if(i != b)
{
//		print("Error i = " + currentPosition[i].currentPosition + "and b = " + currentPosition[b].currentPosition);
print("Error ints i = " + i + "and b = " + b);
//		currentPosition[b].currentPosition = currentPosition[b].currentPosition +1;
}
}
}
}
}
}
``````

and I’m assigning the points like this in another script that is attached to the different players:

``````PositionsMaster.currentPosition[startPosition].points = 99999999 - ((round*100000) + (currentWaypoint * 1000) + 1000 - distanceUntilWaypoint);
``````

I checked like this if the problem might be in that script but in the console it prints the right values that should be used in the position script too:

``````	Debug.Log("startPosition : " + startPosition + " PlayerPoints : " + (99999999 - ((round*100000) + (currentWaypoint * 1000) + 1000 - distanceUntilWaypoint)));
``````

I would be so happy if someone could help me out on that one because I’m getting so frustrated.

Best regards,

Fred

I didn’t read it thoroughly but it may be because you made the class cars position static. static makes it more of a global class that can only have one value. I’d imagine that the last time you set currentPosition, that will be the same value for all the cars. try:

``````public class carsPosition

{

var currentPosition : int;

var points : float;

}
``````

let me know if it works.

p.s
when you use the line `i < currentPosition.length` in your loops. Does currentPosition refer to the variable inside of carsPosition? if so, it’s not an array, and so does not have a length.

if make the class public instead of static it doesn’t work either.

and currentPosition.length refers to:

``````static var currentPosition : carsPosition [] = new carsPosition [6];
``````

Just a small tip when going trough a list and comparing this objects with each other.

``````for(var i : int; i < currentPosition.length; i++)
{
for(var b : int; b < currentPosition.length; b++)
{
if(i != b)
{
// do stuff
}
}
}
``````

Which will check all objects twice.
Do it like this:

``````for (var i:int; i < currentPosition.length - 1; i++)
{
for (var j:int = (i + 1); j < currentPosition.length; j++)
{
// do stuff
}
}
``````

This will only check every combination once.

Regards,

But if I’d do your combination wouldn’t j be 3 if i would be 2? Because then it wouldn’t check for 0 and 1. Please correct me if I’m wrong

Nobody has a solution for me?

Also why is InvokeRepeating outside of a method?

it’s written like that in the script reference. I tried it without InvokeRepeating, calling the function Update so that it’s called every frame but that did change it.