- Home /
Multifiring functions c#
I have a simple piece of code here, that should only fire once. but for some reason, it activates 'Newfirst' three times before it stops. Anyone have some insight as to why?
if(keepchecking==0){
keepchecking=1;
if(CarGUI.WhichPlace==1)
Newfirst();
if(CarGUI.WhichPlace==2)
Newsecond();
if(CarGUI.WhichPlace==3)
Newthird();
}
(Full Script Below as requested)
private int keepchecking = 0;
private int mapchoice=1;
private int currentfirst;
private int currentsecond;
private int currentthird;
private string currentfirsttime;
private string currentsecondtime;
private string currentthirdtime;
private string currentfirstuser;
private string currentseconduser;
private string currentthirduser;
void Start ()
{
mapchoice = Starting.mapchoice;
currentfirst=PlayerPrefs.GetInt("TimeFirst"+mapchoice);
currentsecond=PlayerPrefs.GetInt("TimeSecond"+mapchoice);
currentthird=PlayerPrefs.GetInt("TimeThird"+mapchoice);
currentfirsttime=PlayerPrefs.GetString("FirstPlace"+mapchoice);
currentsecondtime=PlayerPrefs.GetString("SecondPlace"+mapchoice);
currentthirdtime=PlayerPrefs.GetString("ThirdPlace"+mapchoice);
currentfirstuser=PlayerPrefs.GetString("FirstPlaceUser"+mapchoice);
currentseconduser=PlayerPrefs.GetString("SecondPlaceUser"+mapchoice);
currentthirduser=PlayerPrefs.GetString("ThirdPlaceUser"+mapchoice);
if(keepchecking==0){
keepchecking=1;
if(CarGUI.WhichPlace==1)
Newfirst();
if(CarGUI.WhichPlace==2)
Newsecond();
if(CarGUI.WhichPlace==3)
Newthird();
}
}
void Update() {
print (keepchecking);
print (CarGUI.WhichPlace);
}
void Newfirst()
{
print ("FirstFire");
PlayerPrefs.SetInt("TimeThird"+mapchoice,currentsecond);
PlayerPrefs.SetInt("TimeSecond"+mapchoice,currentfirst);
PlayerPrefs.SetInt("TimeFirst"+mapchoice,CarGUI.finaltime);
PlayerPrefs.SetString("ThirdPlace"+mapchoice,currentsecondtime);
PlayerPrefs.SetString("SecondPlace"+mapchoice,currentfirsttime);
PlayerPrefs.SetString("FirstPlace"+mapchoice,CarGUI.singlelaptime);
PlayerPrefs.SetString("ThirdPlaceUser"+mapchoice,currentseconduser);
PlayerPrefs.SetString("SecondPlaceUser"+mapchoice,currentfirstuser);
PlayerPrefs.SetString("FirstPlaceUser"+mapchoice,OpeningScene.Username);
}
void Newsecond()
{
print ("SecondFire");
PlayerPrefs.SetInt("TimeThird"+mapchoice,currentsecond);
PlayerPrefs.SetInt("TimeSecond"+mapchoice,CarGUI.finaltime);
PlayerPrefs.SetString("ThirdPlace"+mapchoice,currentsecondtime);
PlayerPrefs.SetString("SecondPlace"+mapchoice,CarGUI.singlelaptime);
PlayerPrefs.SetString("ThirdPlaceUser"+mapchoice,currentfirstuser);
PlayerPrefs.SetString("SecondPlaceUser"+mapchoice,OpeningScene.Username);
}
void Newthird()
{
print ("ThirdFire");
PlayerPrefs.SetInt("TimeThird"+mapchoice,CarGUI.finaltime);
PlayerPrefs.SetString("ThirdPlace"+mapchoice,CarGUI.singlelaptime);
PlayerPrefs.SetString("ThirdPlaceUser"+mapchoice,OpeningScene.Username);
}
}
You will need to add more information in order for people to help you solve your problem.
Information needed: Where is this if check happening? What happens in the methods Newfirst, Newsecond, and Newthird.
I've added the full script, though it's mostly just boring playerpref stuff.
Is your script on multiple objects? That could explain why it runs 3 times NewFirst()
Well, I added this (commenting all the code inside the "New*" functions except the print debug) and tried very many times to start/stop the editor and it was never printed more than one time, so it's most likely something else in your other logic scripts. Try to add print(gameObject.name) in the Start function, comment the Update and finally look at the stack trace in your console window when you start to verify what/when are the calls co$$anonymous$$g from, there's definitely no issues in this code.
The stack trace shows the order of calls, so I don't know what you're seeing or interpreting.
Here is the code I'm trying (import in your project, create a blank scene and attach it to the camera or a new gameobject):
using UnityEngine;
using System.Collections;
public class Test : $$anonymous$$onoBehaviour {
private int keepchecking = 0;
class CarGUI1 {
public static int WhichPlace = 1;
}
void Start ()
{
if(keepchecking==0){
keepchecking=1;
if(CarGUI1.WhichPlace==1)
Newfirst();
if(CarGUI1.WhichPlace==2)
Newsecond();
if(CarGUI1.WhichPlace==3)
Newthird();
}
}
void Newfirst()
{
print ("FirstFire");
}
void Newsecond()
{
print ("SecondFire");
}
void Newthird()
{
print ("ThirdFire");
}
}
It prints "FirstFire" only once on me, see if it's the same for you, in which case there's really something else in your other scripts that is interfering. $$anonymous$$aybe you could export your demo scene with all dependencies? as I see here the only dependencies should be the scripts CarGUI, Starting and OpeningScene, it's the only way to understand what's going on else we're walking in the darkness