Question by
ICarb0n · Aug 26, 2021 at 03:08 PM ·
scripting problemspritescripting beginnerspriterenderer
assigning sprites to game object via script
Hi, im following a tutorial on youtube 'Unity Base Building Game'. The program so far has a few classes, an array of tiles of one of two types (at random for now) is created, each tile gets a game object, each game object gets a sprite renderer, then should get a sprite assigned. My issue is only one type of tile is getting a sprite assigned.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameWorldController : MonoBehaviour
{
public Sprite groundSprite;
public Sprite waterSprite;
Map map;
// Start is called before the first frame update
void Start()
{
map = new Map();
for (int x = 0; x < map.Width; x++)
{
for (int y = 0; y < map.Height; y++)
{
Tile tile_data = map.GetTileAt(x, y);
GameObject tile_go = new GameObject();
tile_go.name = "Tile_ " + x + "_" + y;
tile_go.transform.position = new Vector3(tile_data.X, tile_data.Y, 0);
tile_go.AddComponent<SpriteRenderer>();
tile_data.RegisterTileTypeChangedCallback( (tile) => { OnTileTypeChanged(tile, tile_go); } );
}
}
map.RandomizeTiles();
}
// Update is called once per frame
void Update()
{
}
void OnTileTypeChanged(Tile tile_data, GameObject tile_go)
{
if (tile_data.Type == Tile.TileType.Ground)
{
tile_go.GetComponent<SpriteRenderer>().sprite = groundSprite;
Debug.Log("groundsprite added");
}
else if (tile_data.Type == Tile.TileType.Water)
{
tile_go.GetComponent<SpriteRenderer>().sprite = waterSprite;
Debug.Log("watersprite added");
}
else
{
Debug.LogError("OnTileTypeChanged - Unrecocnised tile type.");
}
}
}
Map class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Map
{
Tile[,] tiles;
int width;
public int Width
{
get
{
return width;
}
}
int height;
public int Height { get
{
return height;
}
}
public Map(int width = 100, int height = 100)
{
this.width = width;
this.height = height;
tiles = new Tile[width, height];
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
tiles[x,y] = new Tile (this, x, y);
}
}
Debug.Log("World created with " + (width * height) + " tiles.");
}
public void RandomizeTiles()
{
Debug.Log("RadomizeTiles");
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if (Random.Range(0, 2) == 0)
{
tiles[x, y].Type = Tile.TileType.Ground;
Debug.Log("Ground!");
}
else
{
tiles[x, y].Type = Tile.TileType.Water;
Debug.Log("Water!");
}
}
}
}
public Tile GetTileAt(int x, int y)
{
if (x > width || x < 0 || y > height || y <0)
{
Debug.LogError("Tile (" + x + "," + y + ") is out of range");
return null;
}
return tiles[x, y];
}
}
Tile class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Tile
{
public enum TileType { Ground, Water };
TileType type;
Action<Tile> cbTileTypeChanged;
public TileType Type
{
get
{
return type;
}
set
{
TileType oldType = type;
type = value;
if (cbTileTypeChanged != null && oldType != type)
{
cbTileTypeChanged(this);
Debug.Log("TileTypeChanged");
}
}
}
LooseObject looseObject;
InstalledObject installedObject;
Map map;
int x;
public int X
{
get
{
return x;
}
}
int y;
public int Y
{
get
{
return y;
}
}
public Tile (Map map, int x, int y)
{
this.map = map;
this.x = x;
this.y = y;
}
public void RegisterTileTypeChangedCallback(Action<Tile> callback)
{
cbTileTypeChanged += callback;
}
public void UnRegisterTileTypeChangedCallback(Action<Tile> callback)
{
cbTileTypeChanged -= callback;
}
}
As you can see ive put some debug logs in and I never see the "groundsprite added" log, i do howver see the "ground!" and "water!" logs, indicating the tile types are indeed changing.
Comment
seems if i add the sprite when i add the sprite renderer to the gameobject it seems to work, we shall see if this causes any issues.