Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ICarb0n · Aug 26, 2021 at 04:59 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

298 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem Changing Sprites with an Array of Sprites 1 Answer

Get sprite size and position 0 Answers

script to swap between images using buttons(next button and previous button) 0 Answers

How Can I Change The Sprite of A Child 1 Answer

How to change a button image with script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges