- Home /
2D Sprite Swap -- HOW?!? (unity 4.3)
Guys... I have been at this for over 7 hours. I cannot, for the life of me, figure out how to swap out textures in the new 4.3 unity sprite editor.
It was super simple before when I had a script handling things, but I figured I would take advantage of the new 4.3 features, and literally redo my entire game. New system, new ways to do things, and therefor less resources to learn.
Does anybody know how to do.... literally everything about this? I'm not looking for a "GetComponent(SpriteRenderer).sprite = newSprite;" response. That's not gonna tell me anything.
If it's even possible, please link me to something that shows me how this kind of thing is done.
Or list off: "use this variable" (IE: var theSpriteYouSwitchTo : SpriteRenderer;) (or whatever, seriously, I have no idea how to set up this new system)
"This script in the start function"
"This script in the update function"
"This script in the Trigger2D function" (Because my character's entire outfit changes (2D sprites) when the character gets a power up.)
I wanna ball into a corner and just die at this point. Why the hell are they making this thing so difficult. Do they NOT think people are going to have powerups in their 2D games?? Am I asking for too much to get an intuitive engine for simple functions? C'MON unity! jesus.
Answer by Hakumaru · Feb 26, 2014 at 08:57 AM
Okay. FIRST!
Gameobject go = Gameobject.FindWithTag("The tag on the object you're changing the sprite of");
that just sets go to equal the gameobject you are tracing, next thing is.
Spriterenderer renderer = go.GetComponent(); //What this says is get the component "Spriterenderer" off of Gameobject "go" and set it to the "renderer" variable.
then you may access that. you may now put something like:
and then in your movement file you set a sprite array "public Sprite[] char1Up;" and place all those sprites in it, and make a timer going down by deltaTime, every second it does "i++" or something, then you can do:
if(Input.getKey(KeyCode.W)){
renderer.sprite = char1UP[i];
}
That says "if the W key is held the sprite well equal whatever picture is currently selected " thats why you do the timer, it changes the picture every second or something like that, and it runs threw all the sprites. you can then do something like
"if(i > 4){
i = 0;
}"
which resets it, this is how i did my walking animations.. Hope it helped! If not, maybe it helped you learn something new!
You lost me at "Gameobject go"
what is Gameobject go? I've never heard of that. And am I tagging my character?--because the script is already on my character.
Also, if you could perhaps spare my scattered brains and tell me where these functions go specifically (is it a variable? in update? In start? does it matter?) that would do wonders. I have really never used much of the information you just suggested so I'm incredibly lost.
btw, I just did this:
VARIABLE: var star : Sprite; var Diamond : boolean;
UPDATE FUNCTION:
if(Diamond){ gameObject.GetComponent(SpriteRenderer).sprite = star; }
That changed the suit my Sprite was in, but the jumping and running animations weren't brought along for the ride. Just the standing. Do you know why the animations didn't go along with it? The sprites on each sheet are animated the same, but they just are colored differently. I'm not dismissing your original suggestion btw, I just wanted to share what simple tactic worked for me, up until the animations failed to follow.
you place this stuff in Update, i'm sorry.. GameObject go is making a GameObject variable called "go" you can change "go" to anything you want.
See, this is why I don't understand responses... they're so vague. How am I writing this variable under the variable sections, and what will it be used for? var go : ___?
Also, so this is going to go in my player's script, but then also my movment file is also in my player's script.. so just to be clear: This is all going in the player's script?
And correct me if I'm wrong but is public Sprite[] C#? I've never seen brackets before in java
Well, you did realize you need to affect the content of the animator so... anyway must have been the wrong video, thought it was the $$anonymous$$ecanim tutorial. But yes, making animations for sprites is easy and is the way you should do this. When you make an animation for a sprite, an animator is automatically created and added to your sprite. All you need to do then is create parameters in the animator that control which animation is being played. You can then access those parameters though your script. So if you had an animation for a star outfit, and a trigger parameter 'star' that, when true, would activate the star animation:
if(just got a star){
animator.setTrigger(star);
}
Ya know, when asking a question, you should ask the proper thing in the first place, also, i expected you to understand where to place simple things, i gave you the info in great detail and it's "too vague" no, you just have no idea what you're doing.