Search Issue Tracker

By Design

Votes

1

Found in

2018.4

2019.4

2020.3

2020.3.8f1c1

2021.1

2021.2

Issue ID

1337815

Regression

No

Color of Image of a prefab instance is reset to the last hand picked/default color when it was changed with OnInspectorGUI

Scene Management

-

How to reproduce:
1. Open attached project "ColorTest2.zip"
2. Open "SampleScene" scene
3. Select "SetColor" GameObject
4. Change "Color" field to any other color in "Select Color Test" component
5. Enter Play Mode

Expected result: Color of the top-right square is unchanged
Actual result: Color of the top-right square is reset to the previous color

Reproducible with - 2018.4.35f1, 2019.4.27f1, 2020.3.9f1, 2021.1.18f1, 2021.2.0a17

Notes:
Color resets to the last hand picked/default color when entering Play Mode

  1. Resolution Note:

    Please use SerializedObject when dealing with game objects in an custom Editor. This way you don't need to think if the object(s) are prefab instances or not, and you will not experience these problems with losing your uchanges.

    If you choose not use SerializedObject, you have to call PrefabUtility.RecordPrefabInstancePropertyModifications on the object you modify if it is a prefab instance.
    Please check out https://docs.unity3d.com/ScriptReference/PrefabUtility.RecordPrefabInstancePropertyModifications.html for more details.

    In your reproduction project, the changes will be applied using PrefabUtility.RecordPrefabInstancePropertyModifications like this:

    using UnityEngine;
    using UnityEditor;
    using UnityEngine.UI;
    public class SetColorTest : MonoBehaviour
    {
    public Color color;
    public GameObject TargetImagePrefab;
    }

    [CustomEditor(typeof(SetColorTest))]
    public class TestEditor : Editor
    {
    public override void OnInspectorGUI()
    {
    base.OnInspectorGUI();
    if (GUI.changed)
    {
    SetColorTest test = target as SetColorTest;
    test.TargetImagePrefab.GetComponent<Image>().color = test.color;
    PrefabUtility.RecordPrefabInstancePropertyModifications(test.TargetImagePrefab.GetComponent<Image>()); //<--- Additional code
    }
    }
    }

Comments (1)

  1. VagueObscure

    Jun 27, 2021 03:22

    I've experienced this in 2019.4.28. Seems to only happen on Image component of children GameObjects of prefab.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.