- Home /
How to reset a public static integer?
Hey everyone I am making a racing and driving game in which you are awarded cash if you finish first in a race. When the player takes first place in a race, a variable called "public static int FirstPos" changes to 1. This is the code where this change takes place -
void OnTriggerExit(Collider other){
if(other.tag == "CarPos"){
positionDisplay.GetComponent<Text>().text = "Position - 1/2";
FirstPos = 1;
}
}
This variable is again referenced in the Race Finish Trigger, which awards the player $500 if this value equals 1 in the previous script. Here is the code for when the cash is given -
FinalFirstPos = PosUp.FirstPos;
if (FinalFirstPos == 1){
GlobalCash.TotalCash += 500;
PlayerPrefs.SetInt("SavedCash", GlobalCash.TotalCash);
}
This all works fine until i win a race because when I do, the integral value is set to 1 and from then on, even if I lose races I still get cash. How can I fix this? Is there a way to reset that value back to 0? I've tried creating FirstPos = 0 in the first script (which has a similar if statement in the second script which adds 0 cash) but that does not work. Any ideas?
Answer by jmhoubre · Jul 19, 2020 at 12:58 PM
Hello, you can set FinalFirstPos to 0 when you give the cash.
if (FinalFirstPos == 1) {
FinalFirstPos = 0;
GlobalCash.TotalCash += 500;
PlayerPrefs.SetInt("SavedCash", GlobalCash.TotalCash);
}
Good luck
Nope, that does not seem to fix it. I still get $500 when I lose the race :-(
Try to play a race while logging your FirstPos, from what i see I'm not sure you update the position constantly but I feel like you only update the first position but the player becomes the first one.
So what you are saying is that I should put the position script in void Update() ?
Can I see the full code, especially the part where you lose the race?
Yeah of course. This is the script when I finish the race (i.e lose the race) -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Vehicles.Car;
using UnityEngine.UI;
using UnityEngine.Scene$$anonymous$$anagement;
public class RaceFinish : $$anonymous$$onoBehaviour
{
public GameObject $$anonymous$$yCar;
public GameObject FinishCam;
public GameObject View$$anonymous$$odes;
public GameObject Level$$anonymous$$usic;
public GameObject CompleteTrig;
public AudioSource Finish$$anonymous$$usic;
public GameObject BackTo$$anonymous$$ain$$anonymous$$enu;
public GameObject LapTimeStop;
public int FinalFirstPos;
void OnTriggerEnter (){
FinalFirstPos = PosUp.FirstPos;
if ($$anonymous$$odeTime.isTime$$anonymous$$ode == true){
//This is race time mode
}
else
{
this.GetComponent<BoxCollider>().enabled = false;
LapTimeStop.SetActive(false);
$$anonymous$$yCar.SetActive (false);
CompleteTrig.SetActive (false);
CarController.m_Topspeed = 0.0f;
$$anonymous$$yCar.GetComponent<CarController> ().enabled = false;
$$anonymous$$yCar.GetComponent<CarUserControl> ().enabled = false;
$$anonymous$$yCar.SetActive (true);
FinishCam.SetActive (true);
Level$$anonymous$$usic.SetActive (false);
View$$anonymous$$odes.SetActive (false);
Finish$$anonymous$$usic.Play();
BackTo$$anonymous$$ain$$anonymous$$enu.SetActive (true);
}
if (FinalFirstPos == 1){
FinalFirstPos = 0;
GlobalCash.TotalCash += 500;
PlayerPrefs.SetInt("SavedCash", GlobalCash.TotalCash);
}
}
}
And this is the part where the position and FirstPos is set to 1 -
public class PosUp : $$anonymous$$onoBehaviour
{
public GameObject positionDisplay;
public static int FirstPos;
void OnTriggerExit(Collider other){
if(other.tag == "CarPos"){
positionDisplay.GetComponent<Text>().text = "Position - 1/2";
FirstPos = 1;
}
}
}
Your answer
Follow this Question
Related Questions
Custom WindowEditor, but variable sets back to 0 when hit Play 1 Answer
How to reset a variable? 1 Answer
Reset script variable values automatically? 2 Answers
Car Steering Relative to Car Speed 2 Answers