I have followed the method from the few tutorials I could find for changing textures, but i have not had success. My goal is to change to a texture depending on the velocity of the character. The script takes a public input of an array of 3 textures and a renderer. I selected the textures and the renderer for my character. The script changes a variable depending on which texture should be selected, then changes rend.material.mainTexture to that entry into the array. When I run the game, a window shows up in the corner called Sprites-Default (Instance) and the textures switch out perfectly there, but in the game window they do not change. I will attach my full script. Feel free to ignore the movement code, that works fine.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed;
public float jumpHeight;
public Rigidbody2D body;
public Transform groundCheckPoint;
public float groundCheckRadius;
public LayerMask groundLayer;
public Texture[] textures;
public int currentTexture;
public Renderer rend;
private bool isTouchingGround;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
body.velocity = new Vector2(h*speed, body.velocity.y);
if (Input.GetButtonDown("Jump") && isTouchingGround) {
body.velocity = new Vector2(body.velocity.x, jumpHeight);
}
if (body.velocity.x > 0){
currentTexture=1;
//Debug.Log("greater than 0");
}
else if (body.velocity.x < 0){
currentTexture=2;
//Debug.Log("less than 0");
}
else {
currentTexture=0;
//Debug.Log("else");
}
rend.material.mainTexture = textures[currentTexture];
//Debug.Log("should be rendering here");
}
}