Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 acocalypso · Apr 19, 2014 at 01:25 PM · c#sprite

Changing Sprites with Script

Hi everyone,

ive read a few answers here but i couldn't fix my own problem.

I try to load a new Sprite when a condition is reached.

 void SetSprite(){
     
 gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("Goku_erw", typeof(Sprite)) as Sprite;
 }

 void Update()
 {
 if (manager.curScore == 1) 
 {
   SetSprite();
 }
 }

but it doesn't load anything, my current Sprite is gone but wont be replaced with my new one.

Comment
Add comment · Show 7
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 fafase · Apr 19, 2014 at 02:04 PM 0
Share
  gameObject.GetComponent ().sprite

are you missing the type?

avatar image acocalypso · Apr 19, 2014 at 02:16 PM 0
Share

oh sry i forgot to post it:

 void SetSprite()
     {
         gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("Goku_erw", typeof(Sprite)) as Sprite;;
 
     }

This is how it looks like, but it didnt work.

i also used something like Resources.Load("Sprites/Goku_erw", typeof(Sprite))

avatar image Romano · Apr 19, 2014 at 02:30 PM 0
Share

have you checked that the current score is making it to 1 like with:

 print(curScore);

in the update function or something? That's the kinda thing I always get wrong...

avatar image acocalypso · Apr 19, 2014 at 02:38 PM 0
Share

This is working. Im working on a solution for hours now. The main problem is that i dont even get a error message / warning message. When i do Debug.Log(gameObject.GetComponent ().sprite)

Output: Null UnityEngine.Debug:Log(Object) Bird:SetSprite() (at Assets/Scripts/Bird.cs:87) Bird:Update() (at Assets/Scripts/Bird.cs:43)

avatar image Romano · Apr 19, 2014 at 02:42 PM 0
Share

would you need to include sprite renderer in that? :

 Debug.Log(gameObject.GetComponent<SpriteRenderer>.sprite
Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by acocalypso · Apr 19, 2014 at 07:03 PM

ok solved my problem just that simple:

     public Sprite Goku_erw;
 
     public Sprite Goku_ssj1;
 
     public Sprite Goku_ssj3
 
 void SetSprite()
     {
         gameObject.GetComponent<SpriteRenderer> ().sprite = Goku_erw;
     }
 
     void SetSprite1()
     {
         gameObject.GetComponent<SpriteRenderer> ().sprite = Goku_ssj1;
     }

 void Update()
 {
 if (manager.curScore == 100) 
         {
             SetSprite();
         }
 }

now its changing the Sprite.

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
avatar image
0

Answer by ransomink · Apr 19, 2014 at 06:26 PM

You should create 2 animations (from the Animation window): Default and ChangeCondition (or whatever makes sense). The Default animation will have your original/default sprite (drap and drop), and the ChangeCondition animation will have the new sprite (the sprite you're trying to load). Doing this will create 2 states in the Animator (with corresponding names).

In the next steps you need to:

  1. Set Default (the state you created from the animation) as your default state (right-click and select from menu)

  2. Create an integer from the Parameters menu (inside the Animator window). Call it curScore (or whatever relevant)

  3. Make a transition from the Default state to the ChangeCondition state (right-click Default and select from menu)

  4. Select the transition you just created (select the Default state and click on the transition inside of Transitions (from the Inspector window)

  5. Setup the condition that will make the transition (between Default and ChangeCondition) to occur

  • Inside of Conditions (from the Inspector window), change Exit Time to curScore (the integer created from the Parameters window)

  • The rest is self-explanatory. You obviously want it to equal 1, so finish up the conditional statement

Now all you have to do is set the curScore integer (from the Parameters window) to manager.curScore...

 // PRIVATE VARIABLES //
 Animator animator;// Reference to the Animator Component
 
 void Awake ()
 {
     // Set animator as a reference to the Animator Component on the GameObject
     animator = GetComponent < Animator > ();
 }
 
 void Update ()
 
 {
     // Set the curScore integer (inside the Animator Component) equal to the curScore from the manager variable
     animator.SetInteger ( "curScore", manager.curScore );
 }

You should avoid using GameObject.Find and GetComponent inside Update() as it can bog down your game. If you must, find the GameObject in Awake() or Start() and create a reference to the Component and set that reference inside Awake() or Start().

Using this method saves you from checking a condition inside Update(), calling a function, grabbing a Component, and constantly loading a sprite everytime the condition is met. You could also change the sprite back to the Default state by creating a transition from ChangeCondition to Default and setting up the conditions.

P.S. I hope the C# syntax is correct. I use JavaScript and tried to convert it as best I could, sorry...

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
avatar image
0

Answer by SunriseKingdom · May 04, 2014 at 04:47 AM

Maybe this will help

var image : Sprite;

 function Start () {
 GetComponent(SpriteRenderer).sprite = image;
 }
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

22 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

Related Questions

How to change the alpha of a sprite 1 Answer

Multiple Cars not working 1 Answer

How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer

Moving A Sprite Then Returning It To Position? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 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