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

Question Changes made to objects by scripts in edit mode not saving when entering play mode.

Discussion in 'Scripting' started by ggrraahhaamm, Jan 3, 2023.

  1. ggrraahhaamm

    ggrraahhaamm

    Joined:
    Aug 3, 2020
    Posts:
    2
    I have a script which changes the color of an object in edit mode, but when I enter play mode the changes
    don't save. Does anyone have any ideas on how to fix this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEditor.SceneManagement;
    6. using UnityEngine.SceneManagement;
    7. [ExecuteInEditMode]
    8. public class colorEditing : MonoBehaviour
    9. {
    10.    
    11.     public Color colorOne;
    12.     public Color colorTwo;
    13.     public bool isColor1;
    14.     Vector3 mousePos;
    15.     // Start is called before the first frame update
    16.  
    17.  
    18.     private void OnEnable()
    19.     {
    20.         SceneView.duringSceneGui += OnScene;
    21.     }
    22.     void OnScene(SceneView scene)
    23.     {
    24.         Event e = Event.current;
    25.         if(e.type == EventType.MouseDown && e.button == 0)
    26.         {
    27.             mousePos = e.mousePosition;
    28.             float ppp = EditorGUIUtility.pixelsPerPoint;
    29.             mousePos.y = scene.camera.pixelHeight - mousePos.y * ppp;
    30.             mousePos.x *= ppp;
    31.             mousePos = scene.camera.ScreenToWorldPoint(mousePos);
    32.             if (Vector2.Distance(mousePos, transform.position) < 0.5f)
    33.             {
    34.                 changeColor();
    35.             }
    36.         }
    37.         if (e.type == EventType.MouseDown && e.button == 2)
    38.         {
    39.             print("Middle mouse button was pressed");
    40.             saveScene();
    41.         }
    42.     }
    43.     void changeColor()
    44.     {
    45.         if (isColor1)
    46.         {
    47.             isColor1 = false;
    48.         }
    49.         else
    50.         {
    51.             isColor1 = true;
    52.         }
    53.     }
    54.    
    55.     // Update is called once per frame
    56.     void Update()
    57.     {
    58.         if (isColor1)
    59.         {
    60.             gameObject.GetComponent<SpriteRenderer>().color = colorOne;
    61.         }
    62.         else
    63.         {
    64.             gameObject.GetComponent<SpriteRenderer>().color = colorTwo;
    65.         }
    66.     }
    67.     void saveScene()
    68.     {
    69.         if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
    70.         {
    71.             Debug.Log("Scene saved");
    72.         }
    73.         else
    74.         {
    75.             print("Scene not saved");
    76.         }
    77.     }
    78.    
    79. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    I don't see you dirtying the object anywhere (
    UnityEditor.EditorUtility.SetDirty()
    ) so Unity doesn't know the value has changed, and won't write the changes out before entering play mode.

    Whenever you change any values via code that you want to persist you must dirty the object.

    Also you should cache the component reference in OnEnable rather than calling
    GetComponent<T>
    every frame.
     
  3. ggrraahhaamm

    ggrraahhaamm

    Joined:
    Aug 3, 2020
    Posts:
    2
    Thanks a lot, I also noticed that I had to unpack the object from it's prefab.