- Home /
store positions in array
I want top store my car Positions in an array so that i can use them to reset my carafter crash but i am unable to do this. this is my code
Vector3[] positions = new Vector3[10];
void Start()
{
StartCoroutine ("CrashCheck");
}
private IEnumerator CrashCheck(){
while(n<10)
{
yield return new WaitForSeconds(1);
positions [n] = car [GameCOnstant.carNumber].transform.position;
n += 1;
}
n = 0;
}
when i debug my positions it alwasy display(0,0,0).
is the GameCOnstant on each of the cars and each one have their one value 0-9 as a list can not be null or empty and first element is 0.
or on the car controller preferably
Here is what i came up with in 10 $$anonymous$$
public static class Cars{
public static Car carNumber1_Race1 = new Car (0,"$$anonymous$$ichael Schumacher ", Resource.Load<GameObject>("CarsFolder/SchumacherCar1"))
public static Car carNumber2_Race1 = new Car (1,"Lewis Hamilton", Resource.Load<GameObject>("CarsFolder/HamiltonCar_Race1")
public static Car carNumber3_Race1 = new Car (2,"Sebastian Vettel", Resource.Load<GameObject>("CarsFolder/VettelCar1")
public static Car carNumber4_Race1 = new Car (3,"Charles Leclerc", Resource.Load<GameObject>("CarsFolder/LeclercCar_Race1")
public static Car carNumber5_Race1 = new Car (4,"$$anonymous$$y Sirotkin", Resource.Load<GameObject>("CarsFolder/SirotkinCar_Race1")
public static Car carNumber6_Race1 = new Car (5,"Pierre Gasly", Resource.Load<GameObject>("CarsFolder/GaslyCar_Race1")
public static Car carNumber7_Race1 = new Car (6,"Brendon Hartley", Resource.Load<GameObject>("CarsFolder/SchumacherCar1")
public static Car carNumber8_Race1 = new Car (7,"Lance Stroll", Resource.Load<GameObject>("CarsFolder/StrollCar_Race1")
public static Car carNumber9_Race1 = new Car (8,"Antonio Giovinazzi", Resource.Load<GameObject>("CarsFolder/GiovinazziCar1")
public static Car carNumber10_Race1 = new Car (9,"Player", Resource.Load<GameObject>("CarsFolder/PlayerCars"+Selection)
public static Selection;
public static List<Car>CarListForRace1=new List<Car>(){carNumber1_Race1, carNumber2_Race1, carNumber3_Race1, carNumber4_Race1, carNumber5_Race1, carNumber6_Race1, carNumber7_Race1, carNumber8_Race1, carNumber9_Race1, carNumber10_Race1 }
}
public struct Car
{
public int ID;
public string Name;
public GameObject CarPrefab;
public Car(int id){
ID=id;
Name="";
CarPrefab=new GameObject();
}
public Car(int id, string driversName){
ID=id;
Name=driversName;
CarPrefab=new GameObject();
}
public Car(int id, string driversName, GameObject carPrefab){
ID=id;
Name=driversName;
CarPrefab=carPrefab;
}
}
public class Cars$$anonymous$$anager:$$anonymous$$onoBehaviour{
public List<bool>Races new List<bool>(25){true};
public List<Car>CarsInRace = new List<Car>();
public List<GameObject>CarInRacePrefabs=new List<GameObject>();
public int RaceSelection;
void Awake(){
for(int i=0;i<Cars.CarListForRace1.Count;i++){
if(Equals(i,RaceSelection)&&Equals(Races[i],true) ){
CarsInRace =GetCurrentRacesCars(i);
}
}
}
List<CarsInRace>GetCurrentRacesCars(int selection){
if(Equals(selection,Cars.Selection)&&Equals(selection,RaceSelection)){
return Cars.CarListForRace1;
} else{
Debug.LogException(new System.Exception("You forgot to set the selection on the static class."));
}
// to return the correct cars. this script is not complete. will always return same list
}
}
You could always do something like this.
public class TimeController : $$anonymous$$onoBehaviour
{
public class PositionTimeStamp
{
public Vector3 position;
public System.DateTime time;
}
public List<PositionTimeStamp> timeStamps;
public int maxStoredStamps;
void Awake()
{
timeStamps = new List<PositionTimeStamp>();
timeStamps.Add(new PositionTimeStamp() { position = transform.position, time = System.DateTime.Now });
}
void Update()
{
if (isCarCrashed)
return;
if (timeStamps.Count + 1 > maxStoredStamps)
{
timeStamps.Remove(timeStamps[0]);
}
timeStamps.Add(new PositionTimeStamp() { position = transform.position, time = System.DateTime.Now });
}
}
You're not showing us your entire code. I don't see anywhere in your code where the variable 'n' is declared.
Answer by husnain_rao · Feb 17, 2019 at 06:43 PM
hey @zereda-games thanx for your reply. Gameconstant contains the value of selected car number. my question is that i want to store my car position in every second. so that when the car is crashed i can reset my car on some previously saved position.