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 Ghislo · Mar 14, 2016 at 09:03 AM · scripting problemui

How to change button.spriteState.pressedSprite sprite by script - Unity 4.7.1

The title says all, I already tryed everything.

I begin with the error it gives me :

error CS1612: Cannot modify a value type return value of UnityEngine.UI.Selectable.spriteState. Consider storing the value in a temporary variable

and the code that gives that error :

         cont.GetComponent<Button> ().spriteState.pressedSprite = theme.pressbuttons [0];
         pause.GetComponent<Button> ().spriteState.pressedSprite = theme.pressbuttons [1];
         help.GetComponent<Button> ().spriteState.pressedSprite = theme.pressbuttons [2];
        .... etc

First i tryed instantiating a Sprite object and assigning it theme.pressbuttons[] sprite, but it gives the same error.

Then I tryed instantiating a SpriteState object and assigning it the spritestate of the button to change tha sprite, this way it won't give me the error but i don't the get the sprite to be changed

The error tells me to store the value in a temp variable, I tryied for both the variables but it doesn't work either way... What I could try is storing the Button in a temp variable, but it doesn't make sense to me, since I already tried with the attached SpriteState.

I think i'm missing something, the code to change the normal state sprite works because i only have to change the Image().sprite value, but this one accessing the Button().spriteState is not.

The Editor suggest me when I type "pressedSprite" a {get; set;} method but I don't really know how to use it.. Thanks for answers!

Oh, I'm using C# .

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 TechnicPyro · Oct 17, 2020 at 08:08 AM

Sorry to revive such an old thread but this is the second result on Google when I searched for a similar issue I was having.

Played around with it a bit and this is what I found works:

 SpriteState myStates;
 
 myStates.pressedSprite = mySprite;
 
 thisButton.spriteState = myStates;

The compiler doesn't like the direct assignment of values to the properties of the button's sprite state so you have to do it indirectly by creating a SpriteState element and assigning that to the button's sprite state itself.

I hope that makes sense.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers

NullReferenceException on Text element 1 Answer

Highscore - BestTime 1 Answer

UnityEngine.UI is not working. 2 Answers

UI Button Movement Problem 0 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