Search Unity

data transfer does not work

Discussion in 'Scripting' started by Aparty_, Sep 18, 2020.

  1. Aparty_

    Aparty_

    Joined:
    Apr 27, 2020
    Posts:
    41
    I wrote a script to save the settings and a script to change them are two separate files and there is a problem with line 12 of the file "setting". ,
    maybe I made a mistake somewhere.

    setting.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5. using System.IO;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7.  
    8.  
    9.  
    10. public class setting : MonoBehaviour
    11. {
    12.     UIScript uiset = new UIScript();
    13.  
    14.     float volumeS = uiset.volume;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.        
    24.     }
    25. }
    26. [System.Serializable]
    27. public class SaveSetting
    28. {
    29.     public SaveSetting(setting seting)
    30.     {
    31.  
    32.     }
    33. }
    34. public static class SaveLoadSet
    35. {
    36.     private static string path = Application.persistentDataPath + "/settings.sndS";
    37.     private static BinaryFormatter formatter = new BinaryFormatter();
    38.     public static void SaveSettings(setting seting)
    39.     {
    40.         FileStream fs = new FileStream(path, FileMode.Create);
    41.  
    42.         SaveSetting data = new SaveSetting(seting);
    43.  
    44.         formatter.Serialize(fs, data);
    45.  
    46.         fs.Close();
    47.     }
    48.     public static SaveSetting LoadSettings()
    49.     {
    50.         if (File.Exists(path))
    51.         {
    52.             FileStream fs = new FileStream(path, FileMode.Open);
    53.  
    54.             SaveSetting data = formatter.Deserialize(fs) as SaveSetting;
    55.  
    56.             fs.Close();
    57.  
    58.             return data;
    59.         }
    60.         else
    61.         {
    62.             return null;
    63.         }
    64.  
    65.     }
    66.  
    67. }
    UIScript.cs:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5.  
    6. public class UIScript : MonoBehaviour{
    7.     public static float volume = 0;
    8.     public int quality = 0;
    9.     public bool isFullscreen = false;
    10.     public AudioMixer audioMixer;
    11.  
    12.     private Resolution[] resolutions;
    13.     private int currResolutionIndex = 0;
    14.  
    15.     public bool menuTrans = false;
    16.     public bool pauseStatus = false;
    17.     public bool EnventarOpen = false;
    18.     public bool inTransport = false;
    19.     public bool seting = false;
    20.  
    21.     public int torInt = 0;
    22.    
    23.  
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.        
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         if (Input.GetKeyDown(KeyCode.T)) MenuTransF();
    35.         if (Input.GetKeyDown(KeyCode.Escape)) Pause();
    36.         if (Input.GetKeyDown(KeyCode.E)) EnventarBo();
    37.      
    38.     }
    39.     private void OnGUI()
    40.     {
    41.        
    42.        
    43.         /*StartCoroutine(MenuTransport());*/
    44.         if (pauseStatus == true)
    45.         {
    46.             GUI.Label(new Rect(10, 10, 200, 100), "Pause");
    47.             if (GUI.Button(new Rect((float)(Screen.width / 1.5), (float)(Screen.height/20), Screen.width / 6, Screen.height / 8), "Settings"))
    48.             {
    49.                 Debug.Log("Setting");
    50.                 EnventarOpen = false; pauseStatus = false; menuTrans = false; seting = true;
    51.                
    52.             }
    53.         }
    54.         if (seting == true)
    55.         {
    56.            
    57.             //GUI.Label(new Rect(10, 10, 200, 100), "Settings");
    58.            
    59.             if (torInt==0)
    60.             {
    61.                //GUI.Slider(new Rect(25, 25, 100, 30), volume, 10.0f, 0.0f);
    62.                 volume = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), volume, 0.0F, 100.0F);
    63.             }
    64.             if (torInt == 1)
    65.             {
    66.              
    67.             }
    68.             if (torInt == 2)
    69.             {
    70.                
    71.             }
    72.             if (torInt == 3)
    73.             {
    74.              
    75.             }
    76.  
    77.             if (torInt == 4)
    78.             {
    79.                
    80.             }
    81.            
    82.         }
    83.         if (EnventarOpen == true)
    84.         {
    85.  
    86.         }
    87.         if (menuTrans == true)
    88.         {
    89.  
    90.             GUI.Label(new Rect(10, 10, 200, 100), "Transport");
    91.             if (GUI.Button(new Rect((float)(Screen.width / 1.5), (float)(0), Screen.width / 6, Screen.height / 8), "Base"))
    92.             {
    93.                 Debug.Log("hufyfg");
    94.             }
    95.             if (GUI.Button(new Rect((float)(Screen.width / 1.5), (float)(0) + Screen.height / 8, Screen.width / 6, Screen.height / 8), "Player"))
    96.             {
    97.                 Debug.Log("player");
    98.                 inTransport = false;
    99.             }
    100.         }
    101.     }
    102.  
    103.     public void ChangeVolume(float val)
    104.     {
    105.         volume = val;
    106.     }
    107.  
    108.     public void ChangeResolution(int index)
    109.     {
    110.         currResolutionIndex = index;
    111.     }
    112.  
    113.  
    114.  
    115.  
    116.  
    117.     public void QuitGame()
    118.     {
    119.         Application.Quit();
    120.     }
    121.  
    122.     void MenuTransF() { if (true) { menuTrans = !menuTrans; EnventarOpen = false; pauseStatus = false; seting = false; } }
    123.     void EnventarBo() { EnventarOpen = !EnventarOpen; menuTrans = false; pauseStatus = false; seting = false; }
    124.     void Pause() { pauseStatus = !pauseStatus; menuTrans = false; EnventarOpen = false; seting = false; }
    125. }
    126.  
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    We need the error message , not only "there is an error" :D
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If a class inherits from MonoBehaviour, you can't create an instance of it with "new"

    You either have to use AddComponent to add it to a gameobject or GetComponent if you already added it to a gameobject.

    The other option is don't inherit from MonoBehaviour on your UIScript class if it doesn't need to be on a gameobject, but based on the class, that isn't the case.

    I suppose you could split the data you want to save with the methods that need to be on a gameobject, but at this point, I think you should get the basic issue.
     
  4. Aparty_

    Aparty_

    Joined:
    Apr 27, 2020
    Posts:
    41
    the problem to pass variables from one class to another
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    That is not an actionable statement as there are ten million different ways to accomplish this task.

    Let me help you: How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
    Joe-Censored and Brathnann like this.
  6. Aparty_

    Aparty_

    Joined:
    Apr 27, 2020
    Posts:
    41
    I turned because I don't know how to do it that bi it worked.