Hi, I have been trying to understand how to use bools to do this, I am new to programming
public TMP_Text Score;
public int scoreAmount = 0;
private bool isExecuted = true;
// Start is called before the first frame update
void Start()
{
scoreAmount = 0;
Score = GetComponent<TMP_Text>();
scoreAmount = PlayerPrefs.GetInt("scoreKey");
}
void Update()
{
{
Score.text = scoreAmount.ToString();
PlayerPrefs.SetInt("scoreKey", scoreAmount);
}
}
public void AddScore()
{
scoreAmount += 1;
Score.text = "" + scoreAmount;
so bear with me, but how can I prevent the ability to add a score for .55f so that the player cannot double click for extra points? I know that it has something to do with bools and invoke or coroutines, but I cannot figure out how to use a bool essentially.
What code editor are you using? Your formatting is all over the place. And I’ve mentioned before that you should not be setting PlayerPrefs every frame. Just do it once in AddScore when the value gets updated. Or better yet, don’t use PlayerPrefs.
I tried this, with the CancelInvoke function. But, it did nothing, even if it had worked I do not think it would work for me because update only runs every frame rather than ever half frame.
Just saw this, for some reason the start coroutine turns everything red for something missing at the end where the semicolon goes. I really like player prefs, it has been working fine. But the code does help alot to help me figure out how I can make it work. thanks alot!
I am using JetBrains by the way, I really like it. But this is what my entre scoring script looks like with what spiney added. Just in case anyone can throw it in and see why it does not compile.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Unity.PlasticSCM.Editor.WebApi;
using UnityEngine.SceneManagement;
using Unity.VisualScripting;
public class Test : MonoBehaviour
{
public TMP_Text Score;
public int scoreAmount = 0;
// Start is called before the first frame update
void Start()
{
scoreAmount = 0;
Score = GetComponent<TMP_Text>();
scoreAmount = PlayerPrefs.GetInt("scoreKey");
}
void Update()
{
{
Score.text = scoreAmount.ToString();
PlayerPrefs.SetInt("scoreKey", scoreAmount);
}
}
private bool isExecuted = false;
public void AddScore()
{
if (isExecuted == true)
{
return;
}
scoreAmount += 1;
Score.text = "" + scoreAmount;
StartCoroutine(Cooldown(0.55f);
}
private IEnumerator Cooldown(float time)
{
isExecuted = true;
yield return new WaitForSeconds(time);
isExecuted = false;
}
ScoreJSON ScoreNumber = new ScoreJSON();
ScoreNumber.Amount = Score.text;
string json = JsonUtility.ToJson(ScoreNumber, true);
File.WriteAllText(Application.dataPath + "/ScoreNumber.json", json);
}
public void SubtractScore()
{
ScoreJSON ScoreNumber = new ScoreJSON();
ScoreNumber.Amount = Score.text;
string json = JsonUtility.ToJson(ScoreNumber, true);
File.WriteAllText(Application.dataPath + "/ScoreNumber.json", json);
scoreAmount -= 1;
}
public void DeleteScoreKey()
{
}
public void ReturnToMenu()
{
SceneManager.LoadScene(PlayerPrefs.GetInt("SavedScene"));
PlayerPrefs.DeleteKey("scoreKey");
}
private void OnApplicationQuit()
{
PlayerPrefs.DeleteKey("scoreKey");
}
}
Lines 83 to 93 is just completely invalid code too. It’s sitting out in the middle of nowhere. You really need to learn some basic C#. You need to be able to write correct syntax without making forum posts about it.
You still need to be able to write code without syntax errors, alongside being able to identify them yourself and fix them unaided. That is the absolute minimum of basic C# coding.
Probably something else I forgot about, I got it to compile and it did the weirdest thing. It looks like it should work when i was looking up some of the stuff that you put in there, but it disabled the button from add score and then from being reverted back to its old script and disabled the script below it until that script was moved above the score script, but the others continued to work and subtract score. I had to delete that button and duplicate it from another and set it back up as the button that adds score and shuffles scenes. Its tricky.
So, I have tried something different based off of how I have seen bools used to define a function from another forum, and that was by putting the bool in the Start() void and setting it to equal to some function that you want to be able to toggle on or off. And it makes sense, but I still of course cannot get it to work, it just stays the same and continues to add score if you click it really fast during the scene load delay. I tried setting the bool to not equal anything when first stated and then only addressing it to equal something first in the start() void but that did not do anything, of course and then tried setting the bool to equal true when I first stated it, as it was to equal a function to be toggled so that it was active when script was started and then through the coroutine, to put it to false for the scene load delay. But, I have not hit it right yet. Maybe this will tick someone else’s mind, I just thought it could not hurt to post it. I also tried on start to having it just equal the actual text that the score is being relayed to, but that probably would not work even if it did react because it would just delay the points to go on. So, as you can see in the script, I tried telling it to equal add 1 point, and this was the best I could do off of speculation. Even though I am sure it is not correct, by telling the bool to equal and int, the int has to equal a number of some kind.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
using Unity.VisualScripting;
public class Test : MonoBehaviour
{
public TMP_Text Score;
public int scoreAmount = 0;
private bool isActive = true;
// Start is called before the first frame update
void Start()
{
scoreAmount = 0;
Score = GetComponent<TMP_Text>();
scoreAmount = PlayerPrefs.GetInt("scoreKey");
isActive = scoreAmount == +1;
}
void Update()
{
{
Score.text = scoreAmount.ToString();
PlayerPrefs.SetInt("scoreKey", scoreAmount);
}
}
public void AddScore()
{
scoreAmount += 1;
Score.text = "" + scoreAmount;
ScoreJSON ScoreNumber = new ScoreJSON();
ScoreNumber.Amount = Score.text;
string json = JsonUtility.ToJson(ScoreNumber, true);
File.WriteAllText(Application.dataPath + "/ScoreNumber.json", json);
StartCoroutine(routine: Cooldown(.55f));
}
private IEnumerator Cooldown(float time)
{
isActive = false;
yield return new WaitForSeconds(time);
isActive = true;
}
I seriously have no idea what you’re trying, or what you think you’re trying.,
You do absolutely nothing with isActive. It’s completely superfluous now. Did you not understand my very first post with guarding the code from executing?