I used switch statement to change color presets for camera background, particle system and two prefabs. The problem is that the “tile” prefab color loads the desired color, but the “border” prefab color remains the same, and is loaded only after one more scene reload. How to rewrite the code so that prefabs change color at the same time?
using UnityEngine;
using Random = UnityEngine.Random;
public class ColorPresets : MonoBehaviour
{
[SerializeField] private int _numOfPresets = 2;
[SerializeField] private Camera _cameraBackgroundColor;
[SerializeField] private SpriteRenderer _tileColor;
[SerializeField] private SpriteRenderer _borderColor;
[SerializeField] private ParticleSystem _particleColor;
private void Awake()
{
ChangeColors();
}
private void ChangeColors()
{
var colorPreset = Random.Range(0, _numOfPresets);
var main = _particleColor.main;
switch (colorPreset)
{
case 0: // White/gray.
_cameraBackgroundColor.backgroundColor = new Color(0.670f, 0.635f, 0.635f);
_tileColor.color = new Color(0.207f, 0.207f, 0.207f);
_borderColor.color = new Color(0.207f, 0.207f, 0.207f);
main.startColor = new Color(0.207f, 0.207f, 0.207f, 0.003f);
break;
case 1: // Yellow/RaisinBlack
_cameraBackgroundColor.backgroundColor = new Color(1f, 0.929f, 0.396f);
_tileColor.color = new Color(0.117f, 0.129f, 0.168f);
_borderColor.color = new Color(0.117f, 0.129f, 0.168f);
main.startColor = new Color(0.117f, 0.129f, 0.168f, 0.003f);
break;
}
}
}