- Home /
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.
gameObject.GetComponent ().sprite
are you missing the type?
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))
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...
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)
would you need to include sprite renderer in that? :
Debug.Log(gameObject.GetComponent<SpriteRenderer>.sprite
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.
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:
Set Default (the state you created from the animation) as your default state (right-click and select from menu)
Create an integer from the Parameters menu (inside the Animator window). Call it curScore (or whatever relevant)
Make a transition from the Default state to the ChangeCondition state (right-click Default and select from menu)
Select the transition you just created (select the Default state and click on the transition inside of Transitions (from the Inspector window)
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...
Answer by SunriseKingdom · May 04, 2014 at 04:47 AM
Maybe this will help
var image : Sprite;
function Start () {
GetComponent(SpriteRenderer).sprite = image;
}
Your answer
Follow this Question
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