Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
This question was closed May 22, 2021 at 02:21 PM by Zapel1 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Zapel1 · Apr 26, 2021 at 08:55 PM · classessaveload

How do I read and write non-static variables from a different class?

I am making a game where i save the player's settings in a json file. But i cant figure out how to access the variables in the classes i save. The variables cant be static because then i cant save them.

The save script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 
 public static class SaveManager
 {
     public static void save(Total total)
     {
         string dir = Application.persistentDataPath + "/SaveData/";
         
         if(!Directory.Exists(dir))
         {
             Directory.CreateDirectory(dir);
         }
         
         string json = JsonUtility.ToJson(total, true);
         File.WriteAllText(dir + "Saves.json", json);
     }
     
     public static Total load()
     {
         string fullPath = Application.persistentDataPath + "/SaveData/" + "Saves.json";
         Total total = new Total();
         
         if(File.Exists(fullPath))
         {
             string json = File.ReadAllText(fullPath);
             total = JsonUtility.FromJson<Total>(json);
         }else
         {
             Debug.Log(fullPath + "Doesnt exist");
         }
         
         return total;
     }
 }

The script with save and load functions and the classes to save:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SaveScript : MonoBehaviour
 {
    public Total total;
    
    public void save()
    {
         SaveManager.save(total);
         Debug.Log("saved");
    }
    
    public void load()
    {
         total = SaveManager.load();
         Debug.Log("loaded");
    }
    
 }
 
 [System.Serializable]
 public class Settings
 {
     public int quality = 2;
     public int fullscreenMode = 0;
     public float masterVolume = 1f;
     public float musicVolume = 0.5f;
 }
 
 [System.Serializable]
 public class SystemVars
 {
     public int yes;
 }
 
 [System.Serializable]
 public class Info
 {
     public string yes = "nothing yet";
 }
 
 [System.Serializable]
 public class Total
 {
     public Settings Settings;
     public SystemVars System;
     public Info Info;
 }

And the settings script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio; using UnityEngine.UI;

 public class SetSettings : MonoBehaviour
 {
     public AudioMixer audioMixer;
     
     public SaveScript saveScript;
     
     void Start()
     {    
         saveScript = GameObject.Find("Save").GetComponent<SaveScript>();
         saveScript.load();
         setAll();
     }
     
     public void setQuality(int qualityIndex)
     {
         QualitySettings.SetQualityLevel(qualityIndex, true);
     }
     
     public void setFullscreenMode(int fullscreenMode)
     {
         var nothing = fullscreenMode switch
         {
             0 => Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen,
             1 => Screen.fullScreenMode = FullScreenMode.FullScreenWindow,
             2 => Screen.fullScreenMode = FullScreenMode.MaximizedWindow,
             3 => Screen.fullScreenMode = FullScreenMode.Windowed,
             _ => Screen.fullScreenMode = FullScreenMode.FullScreenWindow
         };
     }
     
     public void setMasterVolume(float masterVolume)
     {
         GameObject.Find("Canvas").GetComponent<SetText>().setMasterVolumeText();
         audioMixer.SetFloat("masterVolume", Mathf.Log10 (masterVolume) * 20);
     }
     
     public void setMusicVolume(float musicVolume)
     {
         GameObject.Find("Canvas").GetComponent<SetText>().setMusicVolumeText();
         audioMixer.SetFloat("musicVolume", Mathf.Log10 (musicVolume) * 20);
     }
     
     public void restoreDefaults()
     {
         setQuality(2);
         setFullscreenMode(0);
         setMasterVolume(1f);
         setMusicVolume(0.5f);
     }
     
     public void setAll()
     {    //Take the values from settings class and assign them in functions
             setQuality(s.quality);
         //    Debug.Log(s.quality);
             setFullscreenMode(s.fullscreenMode);
         //    Debug.Log(s.fullscreenMode);
             setMasterVolume(s.masterVolume);
         //    Debug.Log(s.masterVolume);
             setMusicVolume(s.musicVolume);
         //    Debug.Log(s.musicVolume);
         //    GameObject.Find("Canvas").GetComponent<SetText>().quality.value = s.quality;
         //    GameObject.Find("Canvas").GetComponent<SetText>().fullscreenMode.value = s.fullscreenMode;
         //    GameObject.Find("Canvas").GetComponent<SetText>().masterVolumeSlider.value = s.masterVolume;
         //    GameObject.Find("Canvas").GetComponent<SetText>().musicVolumeSlider.value = s.musicVolume;
     }
 }

Can someone please tell me how i can change and read the files in the Savescript class from the SetSettings class?

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

  • Sort: 
avatar image
0

Answer by logicandchaos · Apr 27, 2021 at 01:56 PM

You need to supply a reference to the instance of the object that has the variable you want to access.

YourClass yourclass; yourclass.variableToAccess;

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

116 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

Related Questions

Abstracting class vars, accessing vars by variable 0 Answers

How to reassign a variable's class 1 Answer

class inheriting Vector3 without using monobehavior? 1 Answer

C# Class and Array 1 Answer

Cleaning Up Classes In Lists 1 Answer


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