Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Texture changing in Sprites-Default (Instance) but not game preview

Discussion in '2D' started by dylanchapell, May 15, 2020.

  1. dylanchapell

    dylanchapell

    Joined:
    May 15, 2020
    Posts:
    2
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public float jumpHeight;
    9.     public Rigidbody2D body;
    10.     public Transform groundCheckPoint;
    11.     public float groundCheckRadius;
    12.     public LayerMask groundLayer;
    13.     public Texture[] textures;
    14.     public int currentTexture;
    15.     public Renderer rend;
    16.     private bool isTouchingGround;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.       isTouchingGround = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, groundLayer);
    27.       float h = Input.GetAxisRaw("Horizontal");
    28.       float v = Input.GetAxisRaw("Vertical");
    29.       body.velocity = new Vector2(h*speed, body.velocity.y);
    30.  
    31.       if (Input.GetButtonDown("Jump") && isTouchingGround) {
    32.         body.velocity = new Vector2(body.velocity.x, jumpHeight);
    33.       }
    34.       if (body.velocity.x > 0){
    35.         currentTexture=1;
    36.         //Debug.Log("greater than 0");
    37.       }
    38.       else if (body.velocity.x < 0){
    39.         currentTexture=2;
    40.         //Debug.Log("less than 0");
    41.       }
    42.       else {
    43.         currentTexture=0;
    44.         //Debug.Log("else");
    45.       }
    46.       rend.material.mainTexture = textures[currentTexture];
    47.       //Debug.Log("should be rendering here");
    48.     }
    49.  
    50. }
    51.  
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    You shouldn't be changing the texture for a sprite, you should be accessing SpriteRenderer and changing the sprite directly. No need to access the material at all, just set SpriteRenderer.sprite to the desired sprite.
     
  3. dylanchapell

    dylanchapell

    Joined:
    May 15, 2020
    Posts:
    2
    Ok thanks. Got it by changing public Renderer to SpriteRenderer and Texture[] to Sprite[]