Texture load using string to int replace issue

69 Views Asked by At

The issue according to my debug log is that my ints counts with no problem however the int to string conversion continues to apply to the original value not the updated counter on runtime. (there are some unused private's here for testing) & the frame values are all good. a screen shot of my debug log: http://c2n.me/39GlfuI - as you can see the counter increases but 'frame' doesn't.

Hopefully this is self explanatory

using UnityEngine;
using System.Collections;
public class imagecycle : MonoBehaviour
{
    public string Startingframe;
    private string Nextframe;
    private int framecomp = 0;
    private int frameint;
    private int framestep = 1;
    private int maxframe = 119; 
    private string framestring;

    // Use this for initialization
    void Start ()
    {
        Nextframe = ("frame_000");
        frameint = 20;   // currently adding one to this and resetting on update
    }

    // Update is called once per frame
    void Update ()
    {
        frameint += framestep;

        //Converts framestring to int of frameint -updating frame  
        framestring = frameint.ToString();

        Debug.Log (frameint);

        // replaces loaded texture recourse with frame string:
        Nextframe = Nextframe.Replace ("000", framestring);
        // Loads texture into Currentframe:
        Texture Currentframe = Resources.Load (Nextframe) as Texture;
        // Applies texture:
        renderer.material.mainTexture = Currentframe;
        Debug.Log (Currentframe);

        if (frameint > 119) 
        {
            frameint = 1;
        }
    }   

    void LateUpdate()
    {
    }
}
2

There are 2 best solutions below

2
On BEST ANSWER

that is because in first your next frame is "frame_000" so the replace method will replace 000 with 21 as you can see but after that your nextFrame variable is "frame_21" so there is no "000" in your string so your replace method wont do anything so nextFrame will stay at frame_21

Nextframe = Nextframe.Replace ("000", framestring); wont do anything after the first replace because its string doesnt conatins 000

2
On

Ah many thanks, so it was my understanding of the replace function that was incorrect, I assumed it would reset to frame_000 on on each update. Many thanks guys. And yeah i'll try making it more efficient. Also ,sorry I can't vote up yet; not enough 'rep'.