Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ThaShadow · Dec 14, 2017 at 09:02 PM · playerprefsarraysforeach

Problems with PlayerPrefs or something else

So i am a COMPLETE noob at this, i followed a few tutorials on how to make a clicker game and then made the rest by myself. I was trying to make it so the game saves all my stats with playerprefs (money, money per second e.t.c) I got everything to save except some of my upgrade buttons. I couldn't make it so it saves their count or their price. I got it to work on my other upgrade buttons ( the ones that add money per sec), but not for the ones that add money per click ( i tried saving them like i saved my other upgrade buttons, because the code for them is almost identical). I will add the scripts of my buttons ( both types ) and how i save the data. I know i am probably missing something obvious and i'm sorry if i am. Thanks!

Also the game is a joke and that's why some variables are called "depression" :D

The Script Of How I Save And Load The Buttons:

 using UnityEngine;
 using System.Collections;
 
 public class testt : MonoBehaviour
 {
     public Click click;
     public ItemManager[] items;
     public UpgradeManager[] items2;
 
     public void load()
     {
         float olddepression = PlayerPrefs.GetFloat("depression");
         click.depression = olddepression;
 
         int olddpc = PlayerPrefs.GetInt("dpc");
         click.depressionperclick = olddpc;
 
         foreach(ItemManager item in items)
         {
             item.count = PlayerPrefs.GetInt(item.itemName);
         }
 
         foreach (UpgradeManager item in items2)
         {
             item.count = PlayerPrefs.GetInt(item.upgradeName);
         }
 
         foreach (UpgradeManager item in items2)
         {
             item.cost = PlayerPrefs.GetFloat(item.upgradeName + "2");
         }
 
         foreach (ItemManager item in items)
         {
             item.cost = PlayerPrefs.GetFloat(item.itemName + "2");
         }
 
     }
 
     public void save()
     {
         float olddepression = click.depression;
         PlayerPrefs.SetFloat("depression", olddepression);
 
         float olddpc = click.depressionperclick;
         PlayerPrefs.SetFloat("doc", olddpc);
 
         foreach (ItemManager item in items)
         {
             PlayerPrefs.SetInt(item.itemName, item.count);
         }
 
         foreach (ItemManager item in items)
         {
             PlayerPrefs.SetFloat(item.itemName + "2", item.cost);
         }
 
         foreach (UpgradeManager item in items2)
         {
             PlayerPrefs.SetInt(item.upgradeName, item.count);
         }
 
         foreach (UpgradeManager item in items2)
         {
             PlayerPrefs.SetFloat(item.upgradeName + "2", item.cost);
         }
     }
 }

The Script Of The Buttons I Don't Know How To Save:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class UpgradeManager : MonoBehaviour {
 
     public Click click;
     public UnityEngine.UI.Text upgradeInfo;
     public float cost;
     public int count;
     public int clickPower;
     public string upgradeName;
     public Color standart;
     public Color affordable;
     private float baseCost;
     private Slider _slider;
 
     void Start(){
         baseCost = cost;
         _slider = GetComponentInChildren<Slider> ();
     }
 
     void Update() {
         upgradeInfo.text = upgradeName + "\nCost: " + CurrencyConverter.Instance.GetCurrencyIntoString(cost, false, false) + "\nPower: +" + CurrencyConverter.Instance.GetCurrencyIntoString(clickPower, false, false);
 
         if (click.depression >= cost) {
             GetComponent<Image> ().color = affordable;
         } else {
             GetComponent<Image> ().color = standart;
         }
         _slider.value = click.depression / cost * 100;
 
     }
 
     public void PurchasedUpgrade() {
         if (click.depression >= cost) {
             click.depression -= cost;
             count += 1;
             click.depressionperclick += clickPower;
             cost = Mathf.Round (baseCost * Mathf.Pow (1.15f, count));
         }
     }
 
 }
 

The Script Of The Buttons That I Managed To Save:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ItemManager : MonoBehaviour {
 
     public UnityEngine.UI.Text itemInfo;
     public Click click;
     public float cost;
     public int tickValue;
     public int count;
     public string itemName;
     public Color standart;
     public Color affordable;
     private float baseCost;
     private Slider _slider;
 
     void Start(){
         baseCost = cost;
         _slider = GetComponentInChildren<Slider> ();
     }
 
     void Update(){
         itemInfo.text = itemName + "\nCost: " + CurrencyConverter.Instance.GetCurrencyIntoString(cost, false, false) + "\nDepression: " + CurrencyConverter.Instance.GetCurrencyIntoString(tickValue, false, false) + "/s";
         _slider.value = click.depression / cost * 100;
         if (click.depression >= cost) {
             GetComponent<Image> ().color = affordable;
         } else {
             GetComponent<Image> ().color = standart;
         }
     }
 
     public void PurchasedItem(){
         if (click.depression >= cost) {
             click.depression -= cost;
             count += 1;
             cost = Mathf.Round (baseCost * Mathf.Pow (1.15f, count));
         }
     }
 
 }

Comment
Add comment · Show 2
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 ShadyProductions · Dec 14, 2017 at 09:02 PM 0
Share

where do you call save?

avatar image ThaShadow ShadyProductions · Dec 16, 2017 at 07:05 PM 0
Share

@ShadyProductions

?

in the first script

 foreach (Upgrade$$anonymous$$anager item in items2)
          {
              item.count = PlayerPrefs.GetInt(item.upgradeName);
          }

is that what you meant?

or do you mean from where i call the function? if so i call it from an autosave script which saves the data every second, but ive tried to call it from many things before but the upgrade buttons still dont work.

if you need the autosave script just ask :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by danyx1980 · Dec 14, 2017 at 09:29 PM

itemName and upgradeName are strings but you're loading and saving them as if they were float or int.

Comment
Add comment · Show 1 · 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 ThaShadow · Dec 15, 2017 at 05:55 PM 0
Share

@danyx1980 I know theye are strings im using them as strings aswell because playerprefs is playerprefs.getfloat(string, value) but because i want to save every item in the array ins$$anonymous$$d of typing a string in the string part i type item.itemName which is a string and each item has a different name so i can use that as the name of the key for playerprefs But thanks anyways :) also the one with itemName works fine just not the upgradeName one

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

74 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

Related Questions

Issues creating proper level-unlocking script 0 Answers

How to load an inventory array? 1 Answer

Can someone tell me what is wrong with this? 2 Answers

Player Prefs Mixed Array 1 Answer

How do to you save time for each player and display it? 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