OnParticleCollision sometime doesn't update the gameobject properly

Hi, I write a feature that the color or an object changes every time it collides with a particle,
(from green to red)

The problem is that after calling OnParticleCollision(), the color of some objects does not change in the view. no matter where I put the code that changes the material’s color.
But the color on the inspector has changed.

After some exploring, I found that this can be fixed if I force updating the object by setting active on and off

But it doesn’t sound like a suitable solution.

Is there any setting that will allow me to update the object correctly?


Sorry I could only put one pic in my post.

what do you do with currentColor to apply it to the object?

I update the color in Update()

The MeshRenderer is cached in Start()

    private void Start()
    {
        component = GetComponent<MeshRenderer>();
        initialColor =  component.material.color;
        currentColor = component.material.color;        
    }

image

I’ve already tried edit material.color in OnParticleCollision(), but the results are the same: The color of the material in the inspector is changed to red, but some object changes to red in view after collision, some doesn’t.

OnCollisionEnter() that interact with other objects works fine, every object will update their color in view after the OnCollisionEnter() is called.
However, the objects interact with particle system only around 70% of them gets updated after OnParticleCollision() is called.

public class BoxHit : MonoBehaviour
{
    bool onHit = false;
    float time;
    [SerializeField] Color initialColor;
    [SerializeField] public Color currentColor;
    // Start is called before the first frame update
    MeshRenderer component = null;
    private void Start()
    {
        initialColor = transform.GetComponent<MeshRenderer>().material.color;
        currentColor = transform.GetComponent<MeshRenderer>().material.color;
        component = GetComponent<MeshRenderer>();
    }

    private void OnCollisionEnter(Collision other)
    {
        if (component)
        {
            currentColor = Color.blue;
        }
    }
    private void OnParticleCollision(GameObject other)
    {
        //change currentColor to red
        if (!onHit)
        {
            currentColor = Color.red;
            onHit = true;
            gameObject.SetActive(false);
            gameObject.SetActive(true);
        }
        time = 0.5f;
    }
    private void OnCollisionExit(Collision other)
    {
        currentColor = initialColor;
    }

    private void Update()
    {

        component.material.color = currentColor;

        if (!onHit)
        {
            return;
        }
        if (time > 0)
        {
            time -= Time.deltaTime; return;
        }

        onHit = false;
        //comment out to show which block doesn't get updated properly
        //currentColor = initialColor;
    }


}

The object is generated through Instantiate() with a nested for loop

private void Awake()
    {
        obj = Resources.Load<GameObject>("Prefabs/Box");
        green = Resources.Load("Materials/Green", typeof(Material)) as Material;
        obj.transform.Find("Cube").gameObject.GetComponent<MeshRenderer>().material = green;
    }
    // Start is called before the first frame update
    void Start()
    {
        actions = new PlayerActions();
        actions.Enable();
        n = 10;
        for (int y = 0; y < n + 1; y++)
        {
            for (int x = 0; x < n + 1; x++)
            {
                KeyValuePair<int, int> kp = new(x, y);
                if (set.Contains(kp)) continue;
                Vector3 pos = new(x, 1, y);
                GameObject o = Instantiate(obj, pos, new Quaternion(0, 0, 0, 0));
                array.Add(o);
                set.Add(kp);
                count += 1;
            }
        }
        return; 
    }