Search Issue Tracker
By Design
Votes
0
Found in
2019.4
2020.3
2020.3.18f1
2021.1
2021.2
2022.1
Issue ID
1366047
Regression
No
SetConstantBuffer resets to default when reimporting the script in the Project window if IF statement is false
Reproduction steps:
1. Open the attached project "Demo.zip"
2. Open "BugDemo.unity" Scene
3. In the Hierarchy window select the sphere GameObject
4. In the Inspector window check the "Set Colour" box in the Demo script Component
5. In the Project window right-click on "DummyScript.cs" script and select Reimport
6. Observe the sphere in the Scene view
Expected result: After Reimporting the script, the sphere color stays the same
Actual result: After Reimporting the script the sphere color becomes black
Reproducible with: 2019.4.30f1, 2020.3.18f1, 2021.1.22f1, 2021.2.0b12, 2022.1.0a10
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Crash on ResizeScriptingList<ScriptingObjectPtr> when passing an undeclared variable to the results parameter for GameObject.FindGameObjectsWithTag
- [Android] "Screen.safeArea.y" always returns values outside of the Safe Area when the device is in Portrait orientation
- Frame spike due to many TreeRenderer.TreeUpdated calls when repositioning terrains in large Scenes
- Crash on GameObject::RemoveComponentFromGameObjectInternal when reparenting Text GameObjects
- [IL2CPP-GarbageCollector] Changing GCMode might permanently disable GC in a multithreaded context
Resolution Note:
The behavior seems to be correct. Changes to assets/scripts are not persisted after an update.
So either the setcolor needs to be reapplied on Enable:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class Demo : MonoBehaviour
{
private ComputeBuffer m_computeBuffer;
public Material m_mat;
void OnEnable()
{
m_computeBuffer = new ComputeBuffer(1, 16, ComputeBufferType.Constant);
m_computeBuffer.SetData(new Vector4[]{new Vector4(1, 0, 0, 1)});
// NEW: Copy state to temp field
_SetColourTemp = SetColour;
}
public bool SetColour;
// NEW: Use temp field
private bool _SetColourTemp;
// Update is called once per frame
void Update()
{
Debug.Log($"SetColour {SetColour}");
// NEW: Use temp field
if(_SetColourTemp)
{
_SetColourTemp = false;
m_mat.SetConstantBuffer("buffer", m_computeBuffer, 0, 16);
}
}
}
Or a new material needs to be saved (and DB needs to be informed about the changes), like:
var mat = AssetDatabase.LoadAssetAtPath<Material>( "Assets/Material.mat" );
Undo.RecordObject( mat, "Set Color" );
mat.SetColor( "_Color", Color.red );
EditorUtility.SetDirty( mat );