- Home /
How do I make sprite renderer's sprite change in runtime c#?
Hello, I am trying to generate a random grid map. It is a very basic 2d tiled map consisting of 5 different terrain types. I have loaded the terrains as sprites as such: public Sprite[] terrain; where I added each sprite in Unity Inspector.
My code successfully creates a random map from these sprites. The issue is when I run code to attempt to update the map. The idea is that the code edits the terrain based upon the already generated terrain.
I need to be able to change sprite of the renderer for GameObjects (each tile is a GameObject). I have tried doing this in my code in 2 different ways:
renderer.sprite = terrain[2];
and
renderer.sprite = Resources.Load("Mountain", typeof(Sprite)) as Sprite;
neither of these work in updating the sprite renderer. I have checked the renderer's sprite using:
Debug.Log(tmp.GetComponent().sprite);
where "tmp" is the GameObject. This has shown that the sprite is "Mountain", as it should be. However, when I look on the screen and in the unity inspector the sprite renderer's sprite is still referenced to the original sprite (not "Mountain").
What solution is there for this problem? Has anyone encountered this? Thanks!
$$anonymous$$y first guess would be that there's something wrong with your generation. I suggest opening a seperate scene in which you just have a sprite by default and you try to change that sprite on it's own. If that doesn't work you're doing something wrong with the code you mentioned here, if it does work like you suspect, then there's something wrong with the generation, which is more plausible.
Try to check for overlapping sprites, name your sprites individually ("Sprite1", "Sprite2", or "SpriteXY") upon creation and debug whether the sprites end up correctly where you want them to be and there's none in the same place.
Also, updating the sprites when the editor is paused may not update until playing / updating the ui again.
I hope that helps, if not, perhaps with more details I can assist.
Hi ThePersister,
When I generate my terrain I do so by numbering the object names. Thus I get the names "Terrain1", "Terrain2", "Terrain3", etc. I use an array to store the type of terrain each object is. So if Terrain23 was grass, array[22] = 0, where 0 is associated with the sprite grass. I have used this to debug my entire grid, and each terrain correctly associates with the associated sprite.
Furthermore, I call the function from a button placed on the UI. The function calls another function that generates the terrain, and then another function that edits the terrain. The function that generates works correctly and stores the values in the array. The problem is when the editTerrain() function is called to edit. What is strange about this is that the first time I press the button it works correctly. The terrain generates and edits correctly. After this, every time I click the button it only generates the terrain correctly. It appears as if the terrain did not update at all, but I know that it ran the function (and logic correctly) because of tests with Debug.Log(). The only problem I can identify to not show the update is the sprite not changing.
As I mentioned, when I test in c# it returns the sprites value is $$anonymous$$ountain, but in the inspector it is not.
Thanks for your ideas, I might just try starting from "scratch" working from the ground up to find the problem.
Seems like you've got a good idea of how it should be. If it does work the first time around. then perhaps there's a difference between the first and second time, assu$$anonymous$$g you might've missed a couple places with debugs, if not, I suggest clearing all generation data and removing any instantiated objects before starting the second generation, as a proper reset, if that make sense.
Of course for optimization reasons you could consider applying an "Instance Pool" pattern. Either way, whether you are starting from scratch or not, best of luck! And if I can be of assistance just let me know. :)
Hey @redVeinVeins,
i think i had the same problem a few weeks ago. Try using
Resources.Load("$$anonymous$$ountain", typeof(Sprite))
ins$$anonymous$$d of
Resources.Load("$$anonymous$$ountain", typeof(Sprite)) as Sprite
Dont know why that worked for me.
If that does not work you could try this approach: http://answers.unity3d.com/answers/616467/view.html
A snippet out of the answer above:
SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
if(use skin 1){
skin = texture2D_1;
}else{
skin = texture2D_2;
}
for (int i = 0; i < renderers.Length; i++) {
$$anonymous$$aterialPropertyBlock block = new $$anonymous$$aterialPropertyBlock();
block.AddTexture("_$$anonymous$$ainTex",skin);
renderers[i].SetPropertyBlock(block);
}
Did not try that out though.
Your answer
Follow this Question
Related Questions
Crop/Mask a Sprite on runtime 0 Answers
Why is my sprite not the original resolution? 2 Answers
I have an animaiton problem about sprite loading. 0 Answers
[Unity 2d|C#] Help with changing a sprite by holding down a key? 2 Answers
How to swap sprites? 1 Answer