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 /
  • Help Room /
avatar image
0
Question by nati8oy · Dec 12, 2019 at 04:02 AM · scriptableobjectperformance optimization

How to avoid using Update for scriptable object data?

Hello all! I am making a word game which has letter tiles. The idea is that each time you take a turn, you tiles "age" which essentially means they lose one point of endurance. When their age reaches 0, they are replaced. These letter tiles can have skins for which I'm using a scriptable object. Each age has a different sprite (0-4). I have been trying to find a way to make the tile skin change only on click, however this doesn't seem to work and I am having to use Update instead. This means I am running at least 10 Update events simultaneously which I am sure is affecting performance. Am just wondering if there is an alternative to this that anyone can suggest?

 public TileClass spawnedTile;
 //used for Scriptable Object access
 tileDisplayAccess = GetComponent<TileDisplay>();

 private void Update()
     {
      switch (spawnedTile.age)
             {
                 case 4:
                     //if the age is 4 then use the tiles from the scriptable object within the array
                    tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[4];
                     break;
                 case 3:
                    tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[3];
                     break;
                 case 2:
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[2];
                     break;
                 case 1:
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[1];
                     break;
                 case 0:
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[0];
                     break;
     
             }
 }


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Wilomomo · Dec 12, 2019 at 03:13 PM

You can use get/set to make a change only when you set your value =)

 private TileClass _spawnedTile;
 public TileClass SpawnedTile
     {
         get { return _spawnedTile}
         set
         {
             _spawnedTile = value;
             switch (_spawnedTile.age)
             {
                 case 4:
                     //if the age is 4 then use the tiles from the scriptable object within the array
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[4];
                     break;
                 case 3:
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[3];
                     break;
                 case 2:
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[2];
                     break;
                 case 1:
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[1];
                     break;
                 case 0:
                     tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[0];
                     break;
 
             }
         }
     }

With this code your change appear only when you change SpawnedTile value =)

I hope this will answer your question ^^

Comment
Add comment · Show 2 · Share
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 nati8oy · Dec 14, 2019 at 08:57 AM 0
Share

Thanks for your answer, but that doesn't seem to have worked. Each of the tile sprites don't change at all when they age. They just remain as per the default tile sprite. Any ideas why?

avatar image Bunny83 nati8oy · Dec 15, 2019 at 02:03 AM 0
Share

Well, how does your aging code looks like? If you want things event based of course you have to do your changes when that event happens. If you actually change your age variable somewhere you might want to replace the age variable by a property with backing field and do your logic inside the age setter. So change it when it needs to change. This should be pretty obvious, however we have no idea how your code looks like so we can not offer any alternatives. The first thing I would change is get rid of that switch statement and replace it with this line:

 tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[_spawnedTile.age];

If that age value can get outside the range of 0-4 you might want to add a single if statement around that line to avoid any errors.


However if you have only 10 tiles, running 10 Updates isn't really an issue at all. What could be an issue is the constant pointless re-assigning of the same sprite. However it might have no impact at all but that depends on the exact implementation. If you worry about the performance, measure it. Don't assume things you can not possible know anything about from the outside.

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

196 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

Related Questions

Is ScriptableObject suitable for fast data write/access? 1 Answer

Hotkeys system performance optimization 0 Answers

How to cycle trough enormous amount of entities (DOTS) 1 Answer

Does loading a ScriptableObject on runtime and modifying it affect the ScriptableObject in file? 1 Answer

Scripting Movement Fails for Instantiated Prefab 2 Answers


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