SquidCoin.cs My Currency Script is not working after adding [PlayerPrefs] - c#

I have a currency script for my game that is not displaying how much money the user has.
It was working before I added the PlayerPrefs class.
But after the addition, it will not display anything.
FYI, the PlayerPrefs class allows me to display numbers across multiple scenes in Unity.
I have posted my code below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class squidCoin : MonoBehaviour {
public Text coinDisplay;
public int addMoneyAmmount;
public int squidCoins;
public float saveInterval;
public float paycheckTime;
// Use this for initialization
void Start () {
squidCoins = 50;
PlayerPrefs.SetInt("SquidCoins", squidCoins);
StartCoroutine(moneyADD());
StartCoroutine("SaveMoney");
}
public void squidCoinPayCheck(int squidCoinsToAdd){
squidCoins += squidCoinsToAdd;
}
public void Awake(){
PlayerPrefs.GetInt("SquidCoinsSaves");
}
public void minusSquidCoin(int squidCoinsToSubtract){
if(squidCoins - squidCoinsToSubtract < 0){
Debug.Log ("Oops Hes Broke");
squidCoins += addMoneyAmmount;
}
else{
squidCoins -= squidCoinsToSubtract;
}
}
IEnumerator moneyADD(){
yield return new WaitForSeconds(paycheckTime);
squidCoins += addMoneyAmmount;
}
IEnumerator SaveMoney (){
while (true)
{
yield return new WaitForSeconds(saveInterval);
PlayerPrefs.SetInt("SquidCoinSaves", squidCoins);
}
}
// Update is called once per frame
void Update () {
coinDisplay.text = "You Have: " + PlayerPrefs.GetInt("SquidCoinsSaves");
}
}

i looked at all of your code and it seems there is have many wrong details. I just fixed it and noted your wrongs with comment in front of the wrong parts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class squidCoin : MonoBehaviour {
public Text coinDisplay;
public int addMoneyAmmount;
public int squidCoins;
public float saveInterval;
public float paycheckTime;
// Use this for initialization
void Start () {
squidCoins = 50;
PlayerPrefs.SetInt("SquidCoinsSaves", squidCoins); //your string name was wrong
StartCoroutine(moneyADD());
StartCoroutine("SaveMoney");
}
public void squidCoinPayCheck(int squidCoinsToAdd){
squidCoins += squidCoinsToAdd;
}
public void Awake(){
//PlayerPrefs.GetInt("SquidCoinsSaves"); //you can not get it like this, first you must equal this ti intiger type varible
}
public void minusSquidCoin(int squidCoinsToSubtract){
if(squidCoins - squidCoinsToSubtract < 0){
Debug.Log ("Oops Hes Broke");
squidCoins += addMoneyAmmount;
}
else{
squidCoins -= squidCoinsToSubtract;
}
}
IEnumerator moneyADD(){
yield return new WaitForSeconds(paycheckTime);
squidCoins += addMoneyAmmount;
}
IEnumerator SaveMoney (){
while (true)
{
yield return new WaitForSeconds(saveInterval);
PlayerPrefs.SetInt("SquidCoinsSaves", squidCoins); //string name was wrong
}
}
// Update is called once per frame
void Update () {
coinDisplay.text = "You Have: " + PlayerPrefs.GetInt("SquidCoinsSaves");
}
}

Related

How do I make a unity ad play every 5 rounds/losses?

I have been trying to make my game, play an ad every 5 rounds/losses. I have tried copying some scripts, but they don't work for me. I am a really early developer and don't know much about c# and unity. If you find a solution tell me in which script I need to put the code.
My GameManager script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject gameOverCanvas;
public AdsManager ads;
private void Start()
{
Time.timeScale = 1;
ads.ShowBanner();
}
public void GameOver()
{
gameOverCanvas.SetActive(true);
Time.timeScale = 0;
ads.PlayAd();
}
public void Replay()
{
SceneManager.LoadScene(0);
}
}
My AdsManager script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class AdsManager : MonoBehaviour, IUnityAdsListener
{
#if UNITY_IOS
string gameId = "#######";
#else
string gameId = "#######";
#endif
Action onRewardedAdSuccess;
// Start is called before the first frame update
void Start()
{
Advertisement.Initialize(gameId);
Advertisement.AddListener(this);
ShowBanner();
}
public void PlayAd()
{
if(Advertisement.IsReady("Interstitial_Android"))
Advertisement.Show("Interstitial_Android");
}
public void PlayRewardedAd(Action onSuccess)
{
onRewardedAdSuccess = onSuccess;
if(Advertisement.IsReady("Rewarded_Android"))
{
Advertisement.Show("Rewarded_Android");
}
else
{
Debug.Log("Rewarded ad is not ready!");
}
}
public void ShowBanner()
{
if (Advertisement.IsReady("Banner_Android"))
{
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show("Banner_Android");
}
else
{
StartCoroutine(RepeatShowBanner());
}
}
public void HideBanner()
{
Advertisement.Banner.Hide();
}
IEnumerator RepeatShowBanner()
{
yield return new WaitForSeconds(1);
ShowBanner();
}
public void OnUnityAdsReady(string placementId)
{
Debug.Log("ADS ARE READY!");
}
public void OnUnityAdsDidError(string message)
{
Debug.Log("ERROR: " + message);
}
public void OnUnityAdsDidStart(string placementId)
{
Debug.Log("VIDEO STARTED!");
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
if (placementId == "Rewarded_Android" && showResult == ShowResult.Finished)
{
onRewardedAdSuccess.Invoke();
}
}
}
If you need any more scripts then tell me. I would appreciate any feedback.
Create a counter, increment it after every game and then check if you reached required number of games.
You would need to create counter and required games variables. Then, put the increment and if statement code in your GameOver method:
public void GameOver()
{
gameOverCanvas.SetActive(true);
Time.timeScale = 0;
gameCount++; //increment game count
if(gameCount >= gamesToShowAd) // check if player played enough games to show ad
{
ads.PlayAd();
gameCount = 0; // reset counter
}
}
If you wish, you could consider saving game count using https://docs.unity3d.com/ScriptReference/PlayerPrefs.html or using ready online solution, but it's up to you.

error CS0103: The name 'playfabManager' does not exist in the current context

I just started using Unity, to make a small game called flappy duck.
I wanne have an online leaderboard so everyone can beat each others highscore.
I stumbled on this error Assets\scripts\PlayerController.cs(65,9): error CS0103: The name 'playfabManager' does not exist in the current context
I cand cant find anything wrong. BTW i am using a tutorial
from CoCo Code https://www.youtube.com/watch?v=e2RXDso6fWU&t=266s he uses playfab i am trying to
get this working in my own game.
THIS IS THE SCRIPT THAT MAKES THE TEXT DISPLAY THE HIGHSCORE
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HighSScore : MonoBehaviour
{
public Text HighScore;
// Start is called before the first frame update
void Start()
{
HighScore.text = PlayerPrefs.GetInt("highscore").ToString();
}
}
THIS IS THE PLAYFAB MANAGER
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
public class PlayFabManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Login();
}
void Login()
{
var request = new LoginWithCustomIDRequest
{
CustomId = SystemInfo.deviceUniqueIdentifier,
CreateAccount = true
};
PlayFabClientAPI.LoginWithCustomID(request, OnSuccess, OnError);
}
void OnSuccess(LoginResult result)
{
Debug.Log("Successful login/account create!");
}
void OnError(PlayFabError error)
{
Debug.Log("Error while logging in/creating account!");
Debug.Log(error.GenerateErrorReport());
}
public void SendLeaderboard(int score)
{
var request = new UpdatePlayerStatisticsRequest
{
Statistics = new List<StatisticUpdate>
{
new StatisticUpdate
{
StatisticName = "score",
Value = score
}
}
};
PlayFabClientAPI.UpdatePlayerStatistics(request, OnLeaderboardUpdate, OnError);
}
void OnLeaderboardUpdate(UpdatePlayerStatisticsResult result)
{
Debug.Log("succsessfull send leaderboard");
}
}`
LAST IS THIS THE PLAYERCONTROLLER
were the error is based on line 65
I am trying to send the highscore to the leaderboard in playfab.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
[SerializeField] TextMeshProUGUI scoreText;
Rigidbody2D bird;
int score = 0;
bool dead = false;
int highscore = 0;
// Start is called before the first frame update
void Start()
{
bird = transform.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
public void update ()
{
if (Input.GetKeyDown("space") && !dead)
{
bird.velocity = new Vector2(0, 6f);
}
if (Input.GetKeyDown("r"))
{
SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().buildIndex);
}
if (score > highscore)
{
highscore = score;
SendLeaderboard();
PlayerPrefs.SetInt("highscore", score);
}
}
void OnCollisionEnter2D()
{
dead = true;
score = 0;
scoreText.text = "0";
}
void OnTriggerExit2D(Collider2D col)
{
if (col.gameObject.tag == "PointTrigger")
{
score++;
scoreText.text = score.ToString();
}
}
void OnTriggerExit(Collider col)
{
if (col.gameObject.tag == "PointTrigger")
{
score++;
scoreText.text = score.ToString();
}
}
public void SendLeaderboard()
{
playfabManager.SendLeaderboard(highscore);
}
}
I hope someone can help me.
Most probably in PlayerController you wanted to have a
[SerializeField] private PlayFabManager playfabManager;
and either reference it via the Inspector or at runtime via e.g.
private void Awake ()
{
// Try and get the component if it is attached to the same object as this
if(! playfabManager) playfabManager = GetComponent<PlayFabManager>();
// Or try and find it anywhere in the scene
if(! playfabManager) playfabManager = FindObjectOfType<PlayFabManager>();
// Or simply create and attach it to the same object as this one
if(! playfabManager) playfabManager = gameObject.AddComponent<PlayFabManager>();
}
Or - and in my eyes this would be the more correct solution - the PlayerFabManager should not be a MonoBehaviour at all but rather simply do
public class PlayFabManager
{
public void Login()
{
...
}
...
}
And in PlayerController you'd rather do
private readonly PlayFabManager playfabManager = new PlayFabManager();
private void Start ()
{
playfabManager.Login();
}

Score reset script

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ResetButton : MonoBehaviour
{
public static ResetButton Instance;
public bool isGameOver = false;
public Score score;
public Text scoreText;
public Button yourButton;
internal static object instance;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else if (Instance != this)
{
Destroy(gameObject);
}
}
// Start is called before the first frame update
void Start()
{
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
public void TaskOnClick()
{
ScorePanelUpdater.Score = 0;
}
// Update is called once per frame
void Update()
{
if (isGameOver && Input.GetMouseButtonDown(0))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
I make a quiz game and I want my score to reset when you touch the button reset score. The quiz have different score scripts for 20 questions ( after you finish the first 20 questions , you start another quiz with different score for the next 20 questions ) I want a code that works with a specific Score script ( not to reset all scores from the game in the same time)
ScorePanel16:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ScorePanel16 : MonoBehaviour
{
// better if you can already reference this via Inspector
[SerializeField] Text text;
void Awake()
{
if (!text) text = GetComponent<Text>();
// it's always save to remove listeners before adding them
// makes sure it is added only once
GameInstance16.OnScoreChanged -= OnScoreChanged;
GameInstance16.OnScoreChanged += OnScoreChanged;
// invoke once now with current value
OnScoreChanged(GameInstance16.Score);
}
private void OnDestroy()
{
GameInstance16.OnScoreChanged -= OnScoreChanged;
}
private void OnScoreChanged(int newValue)
{
text.text = "Score: " + newValue;
}
}
GameInstance15:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public static class GameInstance15
{
// Start is called before the first frame update
private static int _score = 0;
// have an event to register listeners
public static event Action<int> OnScoreChanged;
// public property
public static int Score
{
get { return _score; }
set
{
_score = value;
// invoke an event to inform all registered listeners
OnScoreChanged?.Invoke(_score);
}
}
}
Score16:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score16 : MonoBehaviour
{
public Text scoreText;
public Button button;
// Use this for initialization
void Start()
{
DontDestroyOnLoad(gameObject);
if (!button) button = GetComponent<Button>();
button.onClick.AddListener(() => { GameInstance16.Score++; });
GameInstance16.OnScoreChanged -= OnScoreChanged;
GameInstance16.OnScoreChanged += OnScoreChanged;
// invoke once now with current value
Debug.LogFormat("Current Score: {0}", GameInstance16.Score);
OnScoreChanged(GameInstance16.Score);
}
private void OnDestroy()
{
GameInstance16.OnScoreChanged -= OnScoreChanged;
}
private void OnScoreChanged(int newValue)
{
scoreText.text = "Score: " + newValue;
}
// These two make not much sense .. since the value already was public
// you wouldn't need methods for this
// anyway now you could directly do it on GameInformation instead
public void AddScore(int s)
{
GameInstance16.Score += s;
}
public int GetScore()
{
return GameInstance16.Score;
}
}
After a completing a quiz(with 20) questions. Save and add the score in PlayerPref using PlayerPrefs.SetInt("totalScore",score); If you like to reset before completing just clear currentScore.Else if you completed the game ,then score=PlayerPrefs.GetInt("totalScore")+currentScore;.In next line again set the score
using PlayerPrefs.SetInt("totalScore",score);

Unity InvokeRepeating() not working

I have a script in my new clicker game that uses InvokeRepeating:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AutomineController : MonoBehaviour {
static int WoodMiners = 0;
static int StoneMiners = 0;
public Text already_mining;
public Text invalid_transaction;
public Text too_many_miners;
// Error message code
private IEnumerator TooManyMinersCore ()
{
already_mining.gameObject.SetActive (false);
invalid_transaction.gameObject.SetActive (false);
too_many_miners.gameObject.SetActive (true);
yield return new WaitForSeconds (1.5f);
too_many_miners.gameObject.SetActive (false);
}
// The code that does the actual mining
private static void WoodMiner ()
{
Debug.Log("Here");
ChopWood.Add ();
}
// The code that "hires" the miners
public void HireWoodMiner ()
{
if (WoodMiners < 5) {
WoodMiners += 1;
InvokeRepeating("WoodMiner", 0.1f, 3.0f);
} else {
StartCoroutine(TooManyMinersCore());
}
}
}
For some reason, the InvokeRepeating() doesn't work- it should run the WoodMiner() method when the HireWoodMiner() method is run. Why is this?

Putting money.text in my shop scene

I want my money.text to be in my shop scene to buy some shield.
For some reason my money.text is only on the other scene and it's not going in the shop scene.
This is my shop script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ShopController : MonoBehaviour {
int MoneyAmount;
int isPowerup1Sold,isPowerup2Sold,isPowerup3Sold;
public Text MoneyAmountText;
public Button Buynow1,Buynow2,Buynow3;
// Use this for initialization
void Start () {
MoneyAmount = PlayerPrefs.GetInt ("moneyAmount");
}
// Update is called once per frame
void Update () {
MoneyAmountText.text = "Money : " + MoneyAmount.ToString ();
isPowerup1Sold = PlayerPrefs.GetInt ("isPowerup1Sold");
isPowerup2Sold = PlayerPrefs.GetInt ("isPowerup2Sold");
isPowerup3Sold = PlayerPrefs.GetInt ("isPowerup3Sold");
if (MoneyAmount >= 50 && isPowerup1Sold == 0)
Buynow1.interactable = true;
else
Buynow1.interactable = false;
if (MoneyAmount >= 70 && isPowerup2Sold == 0)
Buynow2.interactable = true;
else
Buynow2.interactable = false;
if (MoneyAmount >= 120 && isPowerup3Sold == 0)
Buynow3.interactable = true;
else
Buynow3.interactable = false;
}
public void buyPowerup1()
{
MoneyAmount -= 50;
PlayerPrefs.SetInt ("isPowerup1Sold", 1);
}
public void buyPowerup2()
{
MoneyAmount -= 70;
PlayerPrefs.SetInt ("isPowerup2Sold", 1);
}
public void buyPowerup3()
{
MoneyAmount -= 120;
PlayerPrefs.SetInt ("isPowerup3Sold", 1);
}
public void exitshop()
{
PlayerPrefs.SetInt ("moneyAmount", MoneyAmount);
Application.LoadLevel ("levelselect");
}
}
this is my money script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Money : MonoBehaviour {
public Text MoneyText;
public static int MoneyAmount = 0;
// Use this for initialization
void Start () {
MoneyAmount = PlayerPrefs.GetInt ("moneyAmount",0);
}
// Update is called once per frame
void Update () {
MoneyText.text = "Money" + MoneyAmount.ToString();
}
}
this is my shield.power script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Money : MonoBehaviour {
public Text MoneyText;
public static int MoneyAmount = 0;
// Use this for initialization
void Start () {
MoneyAmount = PlayerPrefs.GetInt ("moneyAmount",0);
}
// Update is called once per frame
void Update () {
MoneyText.text = "Money" + MoneyAmount.ToString();
}
}
This is where i put my money.text to show in the canvas scene.(the highlight blue color).
Text is an UI component for rendering strings on UI. Each scene need to have its own Text-component.
Then you need to keep the amount of money in one place so that every script dealing with money does not have its own MoneyAmount variable.
Money script could look like this:
public class Money : MonoBehaviour {
static int? cachedAmount = null;
const string playerPrefsKeyName = "moneyAmount";
const int startMoney = 0;
public static int Amount {
get {
if (cachedAmount == null) {
cachedAmount = PlayerPrefs.GetInt (playerPrefsKeyName, startMoney);
}
return cachedAmount.Value;
}
set {
if (cachedAmount == null || value != cachedAmount.Value) {
PlayerPrefs.SetInt (playerPrefsKeyName, value);
cachedAmount = value;
}
}
}
}
...and whenever you use money, you could use
if (Money.Amount >= 70) {
Money.Amount -= 70;
MoneyAmountText.text = "Money : " + Money.Amount.ToString ();
}

Categories

Resources