Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Can't change the values of scriptable object

Discussion in 'Scripting' started by Y_E_T, Jul 11, 2022.

  1. Y_E_T

    Y_E_T

    Joined:
    Jan 20, 2020
    Posts:
    28
    Hello, i am trying to change the values of an scriptable object through a script but i can't. I can get the values of scriptable object but i can't change it. Here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PauseMenu : MonoBehaviour
    7. {
    8.     public Slider senSlider;
    9.     public Slider volumeSlider;
    10.  
    11.     public FloatSO floatSO;
    12.  
    13.     private void Start()
    14.     {
    15.         volumeSlider.value = floatSO.Value;
    16.         senSlider.value = floatSO.Value2;
    17.         AudioListener.volume = floatSO.Value;
    18.         Player.GetComponent<PlayerController>().lookSpeedX = floatSO.Value2 * 8.0f;
    19.         Player.GetComponent<PlayerController>().lookSpeedY = floatSO.Value2 * 8.0f;
    20.     }
    21.  
    22.  
    23.     public void AdjustVolume()
    24.     {
    25.         floatSO.Value = volumeSlider.value;
    26.         AudioListener.volume = floatSO.Value;
    27.     }
    28.  
    29.     public void AdjustSensitivity()
    30.     {
    31.         floatSO.Value2 = senSlider.value;
    32.         Player.GetComponent<PlayerController>().lookSpeedX = floatSO.Value2 * 8.0f;
    33.         Player.GetComponent<PlayerController>().lookSpeedY = floatSO.Value2 * 8.0f;
    34.     }
    35.  
    36.     public void Reset()
    37.     {
    38.         volumeSlider.value = 1.0f;
    39.         senSlider.value = 0.25f;
    40.         floatSO.Value = volumeSlider.value;
    41.         floatSO.Value2 = senSlider.value;
    42.         AudioListener.volume = floatSO.Value;
    43.         Player.GetComponent<PlayerController>().lookSpeedX = floatSO.Value2 * 8.0f;
    44.         Player.GetComponent<PlayerController>().lookSpeedY = floatSO.Value2 * 8.0f;
    45.     }
    46. }
    I am just trying to change the "floatSO.Value" and "floatSO.Value2" through slider, slider value is changing when i am sliding it but the float variables of scriptable object floatSO doesn't change. I don't understand why i can't change it.

    Almost i forgot, here is the scriptable object's code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu]
    6. public class FloatSO : ScriptableObject
    7. {
    8.     [SerializeField]
    9.     public float _value;
    10.     [SerializeField]
    11.     public float _value2;
    12.     public float Value
    13.     {
    14.         get { return _value; }
    15.         set { _value = value; }
    16.     }
    17.  
    18.     public float Value2
    19.     {
    20.         get { return _value2; }
    21.         set { _value2 = value; }
    22.     }
    23. }
    24.  
     
  2. Y_E_T

    Y_E_T

    Joined:
    Jan 20, 2020
    Posts:
    28
    I fixed it, it works fine now.
     
  3. theriser777

    theriser777

    Joined:
    Feb 2, 2020
    Posts:
    12
    ...How?
    If you found a solution maybe you should share it so others who have the same problem won't have to go through the same process as you, that's the very point of this forum.
     
  4. Y_E_T

    Y_E_T

    Joined:
    Jan 20, 2020
    Posts:
    28
    ss.PNG
    I just checked these boxes and it worked. You can find them at Project Settings / Editor
     
  5. theriser777

    theriser777

    Joined:
    Feb 2, 2020
    Posts:
    12
    Noted, I'll make sure to do this the next time I come across this problem, thank you.