- Home /
Change Texture2d of sprite within sprite renderer during runtime
So I have a sprite game object with the sprite renderer component. I have set the renderer to use a sprite created from a sprite sheet. the problem im having is during runtime i want to change the sprite to use a new texture.
at the moment im doing something like this and it clears the sprite any tips
Texture2D tex = Resources.Load("planets_2") as Texture2D;
Rect rec = new Rect(0,0,10,10);
Vector2 pivot = new Vector2(0.5f,0.5f);
Sprite newPlanet = Sprite.Create(tex,rec,pivot);
transform.GetComponent().sprite = newPlanet;
as i can not up vote... i would really like to see an answer to this!
Yeah, my friend, I feel your pain. I have the exact same problem:
$$anonymous$$y game has a tile-based system where I have a grid of tiles, and each tile has a list of sprites that represent the different kinds of tiles. When trying to switch between tiles during runtime I use this:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Tile : $$anonymous$$onoBehaviour {
public List<Sprite> spritesList = new List<Sprite>();
private SpriteRenderer myRenderer;
void Start()
{
myRenderer = gameObject.GetComponent<SpriteRenderer>();
}
public void Setup(int imgIndex)
{
if(imgIndex >=1)
{
gameObject.SetActive(true);
myRenderer.sprite = spritesList[imgIndex];
}//if
else if (imgIndex <= 0)
{
gameObject.SetActive(false);
}
// spritesList[imgIndex].SetActive(true);
}//SetupTile
public void UnSetup()
{
for(int i =0; i<spritesList.Count; i++)
{
gameObject.SetActive(false);
}
}//UnsetupTile
void OnTriggerEnter2D(Collider2D hit)
{
}
}
imgIndex is the actual index of the tile that has to be switched to. And unity keeps giving me this message: NullReferenceException UnityEngine.SpriteRenderer.set_sprite (UnityEngine.Sprite value) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/SpritesBindings.cs:176) Tile.Setup (Int32 imgIndex) (at Assets/Standard Assets/Scripts/Tile.cs:21) Grid.Setup (System.Int32[,] gridType) (at Assets/Standard Assets/Scripts/Grid.cs:31) grid$$anonymous$$anager.SetupArrays () (at Assets/Standard Assets/Scripts/grid$$anonymous$$anager.cs:589) grid$$anonymous$$anager.Start () (at Assets/Standard Assets/Scripts/grid$$anonymous$$anager.cs:500)
I've tried EVERYTHING. I've spend my whole afternoon trying to fix it. I checked if I had any unassigned sprite variabe and all, but there seems to be no solution. It must probably be a bug in Unity's editor script or something because it doesn't make sense.
The weirdest thing is that when I first had the idea to use sprites for tiles (before that I was using gameobjects and activating and deactivating them, but the game was running with very low fps), I implemented it, and then I hit play and it ran PERFECTLY. Excited, I connected my android device and clicked on build and run. As soon as the build ran on my device, I notice there was something wrong, the tiles were rendered all wrong and stuff. Switching back to the editor, this error started happening. After the build. And before it was running smoothly with no problems. I didn't change even a character of code. It's unbelievable. $$anonymous$$y eyes hurt from gazing into the screen trying to solve this problem.
The people at Unity must see this so they can analyse if this is an Unity editor bug.
Answer by Carsten-Germer · Nov 22, 2013 at 03:54 PM
Same-ish question here. Would love to see the answer to Kaldhus question.
Solved: Once I got to it, I quickly figured it out. Some code from my prefab that may be of help for future searchers:
I hold the sprites in public variables, so they can be set in the inspector. The Tap... logic is code for using TouchScript.
public class blockHandler : MonoBehaviour {
public Sprite greenSprite;
public Sprite yellowSprite;
public Sprite redSprite;
public int actuState = 3; // default, can be set in inspector
private SpriteRenderer myRenderer;
void Start () {
myRenderer = gameObject.GetComponent<SpriteRenderer>();
if (actuState == 1) {
myRenderer.sprite = greenSprite;
}
else if (actuState == 2) {
myRenderer.sprite = yellowSprite;
}
else if (actuState == 3) {
myRenderer.sprite = redSprite;
}
GetComponent<TapGesture>().StateChanged += tapHandler;
}
void tapHandler (object sender, GestureStateChangeEventArgs e)
{
if (e.State == Gesture.GestureState.Recognized)
{
if (actuState == 3) {
actuState = 2;
myRenderer.sprite = yellowSprite;
}
else if (actuState == 2) {
actuState = 1;
myRenderer.sprite = greenSprite;
}
}
}
}
Answer by vickygroups · Apr 17, 2014 at 09:06 PM
Carsten Germer's answer works. I edited it a little to meet my purposes, and am not allowed to vote yet, but wanted to confirm that it works, if others are wondering.
Answer by TaylorAnderson · Nov 29, 2013 at 06:48 AM
I'm not sure if this helps, but Sprite Renderers take Sprites to render--on your Texture2D in your Assets folder, click the little arrow next to it and you get all the sprites in your spritesheet. Those are Sprites, not the original image which is a Texture2D--those are what the sprite renderer takes as its sprite.