Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gcheema2 · Jul 19, 2020 at 12:09 PM · variablesresetracing gamedriving

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gcheema2 · Jul 19, 2020 at 02:00 PM 0
Share

Nope, that does not seem to fix it. I still get $500 when I lose the race :-(

avatar image Infenix gcheema2 · Jul 19, 2020 at 05:23 PM 0
Share

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.

avatar image gcheema2 Infenix · Jul 20, 2020 at 01:16 AM 0
Share

So what you are saying is that I should put the position script in void Update() ?

avatar image jmhoubre · Jul 19, 2020 at 02:55 PM 0
Share

Can I see the full code, especially the part where you lose the race?

avatar image gcheema2 jmhoubre · Jul 20, 2020 at 01:09 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom WindowEditor, but variable sets back to 0 when hit Play 1 Answer

Variables changed on custom editor or custom editor window reset when unity restart,recompile scripts or play the game 1 Answer

How to reset a variable? 1 Answer

Reset script variable values automatically? 2 Answers

Car Steering Relative to Car Speed 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges