why ExecuteAlways attribute is not working on the first time?

Viewed 72

I'm using unity 2021.3.2f1 personal

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

[ExecuteAlways]
public class DrawOnBoxCollider : MonoBehaviour
{
    public enum DrawStates
    {
        DrawCube,
        DrawWireCube
    }

    public Transform boxColliderToDrawOn;
    [Range(0f, 1f)]
    public float colorTransparency;
    public bool draw = false;
    public DrawStates drawStates;

    private BoxCollider boxCollider = null;

    // Start is called before the first frame update
    void Start()
    {
        if (boxColliderToDrawOn != null)
        {
            boxCollider = boxColliderToDrawOn.GetComponent<BoxCollider>();
        }
        
        drawStates = DrawStates.DrawCube;
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void DrawGizmosOnRunTime(Color color)
    {
        if (boxCollider != null && draw)
        {
            Gizmos.color = color;
            Matrix4x4 rotationMatrix = Matrix4x4.TRS(boxCollider.transform.position, boxCollider.transform.rotation, boxCollider.transform.lossyScale);
            Gizmos.matrix = rotationMatrix;

            switch (drawStates)
            {
                case DrawStates.DrawCube:
                    Gizmos.DrawCube(boxCollider.center, boxCollider.size);
                    break;

                case DrawStates.DrawWireCube:
                    Gizmos.DrawWireCube(boxCollider.center, boxCollider.size);
                    break;
            }
        }
    }

    private void OnDrawGizmos()
    {
        Color c = Color.red;
        c.a = colorTransparency;
        DrawGizmosOnRunTime(c);
    }
}

First time it's not showing anything on the collider but when i'm running the game then it's showing the color drawing on the collider then when i stop the game quit the game it's showing it also in the editor mode, but why i have first to run the game to see it ?

I used a breakpoint and it's getting to the code inside the OnDrawGizmos() with or without the attribute ExecuteAlways but nothing drawn only after running the game and it's drawing in runtime then it will work also in the editor mode.

Maybe it's a bug in this editor version ?

0 Answers
Related