Search Unity

Question Changing material from inside Jobs?

Discussion in 'C# Job System' started by MatanYamin, May 15, 2023.

  1. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    110
    Hey everyone,

    I was wondering if I can change a transform's material from inside an IJobParallelForTransform, and if so, how?

    Thanks!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,998
    A transform doesn't have a material. You mean the MeshRenderer material property? Regardless, Material is a managed object so it's a no. What you can do is generate a list of object/material indexes in a job, and afterwards loop over that on the main thread and make the assignments.

    You can also look into MaterialPropertyBlocks which may be helpful depending on the reason for changing the material.
     
    MatanYamin likes this.
  3. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    110
    Thank you. Yeah that's what I'm doing now. Just wanted to make sure I'm not missing anything :)