A
A
accacha2021-10-20 13:36:03
Unity
accacha, 2021-10-20 13:36:03

The color of the sprites of two prefabs does not change at the same time. What to do?

I wrote a switch to change the presets for the background colors of the camera, effect and two prefabs. The problem is that the color of the tile prefab loads the desired color, but the color of the border prefab remains the same, and is loaded only after another reload of the scene. 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;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Milevsky, 2021-11-13
@Hackerman1

Try to do it with if. case is very unwieldy and hard to read.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question