Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by redVeinVeins · Nov 14, 2016 at 07:36 AM · c#2dspritesspriterenderer

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!

Comment
Add comment · Show 4
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 ThePersister · Nov 14, 2016 at 08:49 AM 0
Share

$$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.

avatar image redVeinVeins ThePersister · Nov 14, 2016 at 05:20 PM 1
Share

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.

avatar image ThePersister redVeinVeins · Nov 15, 2016 at 09:38 AM 0
Share

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. :)

avatar image Desoxi · Nov 14, 2016 at 05:18 PM 0
Share

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.

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

7 People are following this question.

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

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


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