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. Dismiss Notice

Question Why my texture change script doesnt work?

Discussion in '2D' started by Geskawary2341, Aug 14, 2023.

  1. Geskawary2341

    Geskawary2341

    Joined:
    Aug 11, 2023
    Posts:
    9
    So i am making a 2d pixel game and i want my map sprite to update every second. I have 4 different textures, and made a script which changes sprite texture every 1 second, but for some reason it doesnt work. What could be wrong?
    upload_2023-8-15_1-9-33.png
    Here s script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChangeTexture : MonoBehaviour
    6. {  
    7.     public GameObject map;
    8.     public SpriteRenderer SpriteRenderer;
    9.  
    10.     public Sprite map0;
    11.     public Sprite map1;
    12.     public Sprite map2;
    13.     public Sprite map3;
    14.  
    15.     IEnumerator wait1sec(){
    16.         yield return new WaitForSeconds(1);
    17.     }
    18.  
    19.     void UpdateTexture(){
    20.         SpriteRenderer.sprite = map1;
    21.         wait1sec();
    22.         SpriteRenderer.sprite = map2;
    23.         wait1sec();
    24.         SpriteRenderer.sprite = map3;
    25.         wait1sec();
    26.         SpriteRenderer.sprite = map0;
    27.         wait1sec();
    28.     }
    29.  
    30.     void Start(){
    31.         UpdateTexture();
    32.     }
    33. }
     
  2. anilsayar

    anilsayar

    Joined:
    Oct 14, 2018
    Posts:
    14
    Hello,

    Wait1Seconds doesn't work that way. You must write your code below yield return, then a thread will wait for it. Could you please try the following:

    Code (CSharp):
    1.     IEnumerator UpdateTexture(){
    2.         SpriteRenderer.sprite = map1;
    3.         yield return new WaitForSeconds(1);
    4.         SpriteRenderer.sprite = map2;
    5.         yield return new WaitForSeconds(1);
    6.         SpriteRenderer.sprite = map3;
    7.         yield return new WaitForSeconds(1);
    8.         SpriteRenderer.sprite = map0;
    9.     }
    10.  
    11.     void Start(){
    12.         StartCorutine(UpdateTexture());
    13.     }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Why not just make this an animation and be done with it?

    No code, no fuss, no muss, no debugging and especially, no coroutines...
     
  4. Geskawary2341

    Geskawary2341

    Joined:
    Aug 11, 2023
    Posts:
    9
    thank, will try
     
  5. Geskawary2341

    Geskawary2341

    Joined:
    Aug 11, 2023
    Posts:
    9
    well i am new to c# and unity in general. And… I like programming
     
  6. Geskawary2341

    Geskawary2341

    Joined:
    Aug 11, 2023
    Posts:
    9
    Thank you very much! Courutine did only one loop so i added "
    StartCoroutine(UpdateTexture());" in "UpdateTexture()" IEnumerator so it runs for ethenity!