Texture scroll stop when color change
Hi everyone, I create a 3D plateform game. I have a cube which have two differents textures: one for the upper face and one for the others faces. This cube have a little script which scroll the upper texture:
public class ScrollingTexture : MonoBehaviour
{
public float scrollSpeedZ = 0.90f;
public float scrollSpeedX = 0.90f;
public Material mat;
private void FixedUpdate()
{
float offset = Time.time * scrollSpeedZ;
float offset2 = Time.time * scrollSpeedX;
mat.mainTextureOffset = new Vector2(offset2, -offset);
}
}
This works well but the player can also change the materials color of this cube:
public void SetColor(Color color)
{
foreach ( Material m in GetComponent<Renderer>().materials)
{
m.color = color;
}
}
And when SetColor is called, the texture scrolling is stopped... Whereas mat.mainTextureOffset is still evolving !
What is going on? Thanks
Answer by Hellium · May 02, 2019 at 01:10 PM
have you tried to call GetComponent<Renderer>().sharedMaterials
? I believe the problem comes from the fact that calling materials
will instantiate each material, thus the mat.mainTextureOffset
change the offset of the texture on the original material, not on the instances.
Your answer
Follow this Question
Related Questions
I'm trying to get a material on a gameobject to offset slightly over time and can't figure it out. 0 Answers
set normal map texture to HDRP/Lit Material 1 Answer
What right way to SetTextureOffset of material which has dynamic count owners? 0 Answers
Looking for help with creating a gold texture/reflection for mobile games 0 Answers