I made two textures for my 3D game character. I can use both of them by dragging them on my player but I want to do the same through code. I searched and found that people are either using:
1) rend.material.mainTexture = texts [0];
or
2) transform.GetComponent<Renderer>().material.mainTexture = texts [0];
but none of them works for me. I have a mesh renderer attached to my player. I set my code to swap the texture on my player when it reaches a certain amount of points. To debug I attached an empty material to my player and during run time so that I could see the texture on it but the swap didn't take place. The first texture is in texts[0] and the second one is in texts[1].
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class swaptexture : MonoBehaviour {
public Texture[] texts;
public gemlife g;
// Use this for initialization
void Start () {
transform.GetComponent<Renderer> ().material.mainTexture = texts [0];
}
// Update is called once per frame
void Update () {
if (g.ratio >= 0.96f)
transform.GetComponent<Renderer> ().material.mainTexture = texts [1];
}
}


g.ration >= 0.96fis met? \$\endgroup\$GetComponent<MeshRenderer>()instead? \$\endgroup\$