Skip to main content
better question wording and code format
Source Link
user3797758
  • 3.6k
  • 2
  • 29
  • 51

How can I avoid this strange string to change a materials properties in my Unity c# code?

Hello everyone I want to change my materials tiling offset in code, but the only way I found to access it other than drag-dropping to a public field in inspector is to check by name. But I then noticed the name is suffixed with ' (Instance)' .

So it seems a little odd to me having to access it this way, is there another way that I don't know about?

        foreach(Material m in GetComponent<MeshRenderer>().materials)
    {
        if (m.name == "card_front (Instance)")
        {
            m.mainTextureOffset = new Vector2(value * 1/13f, (int)suit * 1/5f);
        }
    }

How can I avoid this strange string to change a materials properties in my Unity c# code?

Hello everyone I want to change my materials tiling offset in code, but the only way I found to access it other than drag-dropping to a public field in inspector is to check by name. But I then noticed the name is suffixed with ' (Instance)' .

So it seems a little odd to me having to access it this way, is there another way that I don't know about?

        foreach(Material m in GetComponent<MeshRenderer>().materials)
    {
        if (m.name == "card_front (Instance)")
        {
            m.mainTextureOffset = new Vector2(value * 1/13f, (int)suit * 1/5f);
        }
    }

How can I change a materials properties in my Unity c# code?

Hello everyone I want to change my materials tiling offset in code, but the only way I found to access it other than drag-dropping to a public field in inspector is to check by name. But I then noticed the name is suffixed with ' (Instance)' .

So it seems a little odd to me having to access it this way, is there another way that I don't know about?

foreach(Material m in GetComponent<MeshRenderer>().materials)
{
    if (m.name == "card_front (Instance)")
    {
        m.mainTextureOffset = new Vector2(value * 1/13f, (int)suit * 1/5f);
    }
}
Source Link
Big T Larrity
  • 1.4k
  • 3
  • 30
  • 60

How can I avoid this strange string to change a materials properties in my Unity c# code?

Hello everyone I want to change my materials tiling offset in code, but the only way I found to access it other than drag-dropping to a public field in inspector is to check by name. But I then noticed the name is suffixed with ' (Instance)' .

So it seems a little odd to me having to access it this way, is there another way that I don't know about?

        foreach(Material m in GetComponent<MeshRenderer>().materials)
    {
        if (m.name == "card_front (Instance)")
        {
            m.mainTextureOffset = new Vector2(value * 1/13f, (int)suit * 1/5f);
        }
    }