0
votes

Hello thank you for reading. I am following a 2048 tutorial and he is using text/color for his tiles. I am using an image.

His code is as follows when applying different styles to the tiles:

TileText.text = TileStyleHolder.Instance.TileStyles [index].Number;
TileText.color = TileStyleHolder.Instance.TileStyles[index].TextColor;
TileImage.color = TileStyleHolder.Instance.TileStyles [index].Tilecolor;

My style holder code is as follows:

 [System.Serializable]
 public class TileStyle 

{
public Sprite tile_number;

}

public class TileStyleHolder : MonoBehaviour
{
//SINGLETON
public static TileStyleHolder Instance;

public TileStyle[] TileStyles;

void Awake() 
{
    Instance = this;
}
}

my Tile code is as follows:

public class Tile : MonoBehaviour
{
public bool mergedThisTurn = false;

public int indRow;
public int indCol;

public int Number 
{
    get 
    {
        return number;
    }
    set 
    {
        number = value;
        if (number == 0)
            SetEmpty();
        else 
        {
            ApplyStyle(number);
            SetVisible();
        }
    }
}
//private SpriteRenderer spriteRenderer;

private Sprite tile_number;

private int number;


void Awake() 
{
    Sprite tile_number = GetComponentInChildren<Image>.sprite ();        
}

void ApplyStyleFromHolder(int index) 
{
    tile_number.image = TileStyleHolder.Instance.TileStyles[index].tile_number;
}

And it throws the error:

  'Sprite' does not contain a definition for 'image' and no accessible extension method 

However, if i use this line of code:

    tile_number = TileStyleHolder.Instance.TileStyles[index].tile_number;

Then it doesn't throw an error, but it also doesn't change the tile style from 2 to 4 once merged. It stays as a 2, and does not throw an error.

Please help, I do now know how to convert the image to the next image in the array.

1

1 Answers

0
votes

tile_number is a pointer to a Sprite.

Sprite doesn't contain a member called sprite, so that's why you get that error.

Now when you do tile_number = something;, you are just changing the pointer (which is tile_number) to point to something else, the thing it was pointing to before doesn't change.

If you take a look at the Sprite API, you will see that it has a member called texture. That's the guy you wanna change.

So, to be clear, when you do tile_number.texture = foo.bar.some_texture, tile_number is a pointer pointing to a Sprite object, this object has a member called texture and you are changing it to foo.bar.some_texture. This is very different from telling your tile_number pointer to point to some other sprite.

I'm assuming the type of tile_number is Sprite, which what your code seems to indicate.