Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 KimJammer · Mar 20, 2020 at 06:13 PM · scripting problemvariablevariablesmultiple objects

non-static Variable in script on multiple gameObjects reset. Why?

I have a script that controls multiple buttons. The buttons are for selecting skins in a 2d game. I have a variable called SkinStatus, which shows if the specific skin is bought, not bought, or equipped. Right after running Load(), which loads in all of the statuses for all of the skins from JSON, the program sets the SkinStatus variable, but it just becomes 0 after the function and functions called from it finish. Since the script is on multiple objects, I need each one to have its own SkinStatus value. Thanks for any help! Please ask questions if you have any!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 using System;
 using TMPro;
 
 public class SkinButton : MonoBehaviour
 {
 
     public GameObject controller;
     private ShopController controllerScript;
 
     //ref to the NotEnoughCoinsScreen
     public GameObject NotEnoughCoinsScreen;
 
     //ref to child text
     private GameObject ButtonText;
     private TextMeshProUGUI ButtonTextBox;
 
     public int SkinStatus;
     public string statusId;
     public string ID;
 
     private int SkinPrice;
     private int currentCoins;
 
     private int justEquipped;
 
     SkinPrices skinPrices = new SkinPrices{
         Skin1 = 0,
         Skin2 = 10,
     };
 
     void Start()
     {
         //Use Transform.Find to just get the child ButtonText, not the other ones.
         ButtonText = transform.Find("ButtonText").gameObject;
         ButtonTextBox = ButtonText.GetComponent<TextMeshProUGUI>();
 
         controllerScript = controller.GetComponent<ShopController>();
 
         justEquipped = 0;
 
         //This is to get the skin's current status (Not bought (1), bought(2), equipped(3)) and
         //make the button say the correct text.
         Load();
     }
 
     public void SetButtonText(int SkinStatus){
         if (SkinStatus == 1){
             ButtonTextBox.text = "Buy";
             ButtonTextBox.fontSize = 50;
         }else if (SkinStatus == 2){
             ButtonTextBox.text = "Equip";
             ButtonTextBox.fontSize = 50;
         }else if (SkinStatus == 3) {
             ButtonTextBox.text = "Equipped";
             ButtonTextBox.fontSize = 30;
         }
     }
 
     public void OnClick()
     {
         //DEBUG!!!! REMOVE LINE BELOW
         PlayerPrefs.SetInt("coins",10);
         Debug.Log(SkinStatus);
 
         //Really weird bug where the SkinStatus will be immideatly scrapped after running Load()
         //Maybe from running mulitple gameObjects off of same script?
         //Runs Load() right before checking SkinStatus so we have accurate information.
         Load();
 
         Debug.Log(SkinStatus);
 
         if (SkinStatus == 1){
             //Uses reflection, to find the correct value in the SkinPrices object.
             //As mentioned, expensive, but simple enough here.
             SkinPrice = Convert.ToInt32(skinPrices.GetType().GetField(ID).GetValue(skinPrices));
             currentCoins = PlayerPrefs.GetInt("coins");
             if (currentCoins >= SkinPrice){
                 //set coins to coins amount minus the skin price and save it. Get it again to get
                 //updated currentCoins amount
                 PlayerPrefs.SetInt("coins",currentCoins - SkinPrice);
                 currentCoins = PlayerPrefs.GetInt("coins");
 
                 controllerScript.UpdateSave(statusId,2);
                 ButtonTextBox.text = "Equip";
                 ButtonTextBox.fontSize = 50;
             }else{
                 NotEnoughCoinsScreen.SetActive(true);
             }
         }else if (SkinStatus == 2){
             controllerScript.UpdateSave(statusId,3);
             ButtonTextBox.text = "Equipped";
             ButtonTextBox.fontSize = 30;
 
             justEquipped = 1;
             Debug.Log(justEquipped + ID);
         }else if (SkinStatus == 3) {
             
         }
     }
 
     public void Load(){
 
         if(File.Exists(Application.persistentDataPath + "/SkinSave.json")) {
             string saveString = File.ReadAllText(Application.persistentDataPath + "/SkinSave.json");
 
             SaveObject saveObject = JsonUtility.FromJson<SaveObject>(saveString);
             //Use reflection to find the correct property in the class. A bit slow,
             //but good enough for now. It's not running mid-game or anything.
             //Also convert the object into int
             int SkinStatus = Convert.ToInt32(saveObject.GetType().GetField(statusId).GetValue(saveObject));
             SetButtonText(SkinStatus);
 
             Debug.Log(statusId+"->"+SkinStatus);
         }else{
             Debug.Log("Uh oh, no save file! From button");
             
             //If this object is the default skin, set it as equipped
             if (ID == "Skin1"){
                 SetButtonText(3);
                 SkinStatus = 3;
             }else{
                 SetButtonText(1);
                 SkinStatus = 1;
             }
         }
     }
 
     public void CheckEquipped()
     {
         Load();
         Debug.Log(justEquipped + ID);
         if (SkinStatus == 3 && justEquipped == 0){
             ButtonTextBox.text = "Equip";
             ButtonTextBox.fontSize = 50;
         }
     }
 
     private class SaveObject {
         public int statusSkin1;
         public int statusSkin2;
     }
 
     //Just a class that holds all of the prices. Uses Skin# as key
     //so the manually inputted values match the prices.
     private class SkinPrices {
         public int Skin1;
         public int Skin2;
     }
 
 }
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
Best Answer

Answer by Namey5 · Mar 21, 2020 at 01:28 AM

At line 114:

 int SkinStatus = Convert.ToInt32(saveObject.GetType().GetField(statusId).GetValue(saveObject));

You don't actually write to the SkinStatus variable. Instead, you declare a local variable of the same name and use that, meaning the script will probably still look like it functions as normal, but you won't actually end up storing the status. If you want to do that, just remove the 'int' type before the assignment.

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 KimJammer · Mar 21, 2020 at 03:12 PM 0
Share

Aah, I did a dumb. Thanks a lot for catching that! I thought it was a different problem altogether.

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

226 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 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

Variables modified on other scripts through a Editor Script reset on Play? 1 Answer

How can I improve my trading script? 1 Answer

I am trying to access a variable from another script. 1 Answer

How to subtract from a variable using a variable from another script 1 Answer

How to increase score for objects that form larger geometries? 0 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