playMaker

Author Topic: Can't change a Sprite Renderer sprite at runtime  (Read 7732 times)

awshima

  • Playmaker Newbie
  • *
  • Posts: 6
Can't change a Sprite Renderer sprite at runtime
« on: September 25, 2014, 09:48:02 AM »
Hello,

Is there a way to change the sprite from a Sprite Renderer component at runtime?

I can drag the Sprite Renderer component to a FSM State to create a Set Property action, and I can drag a Sprite into the Sprite field, but that's all.

The other option besides dragging the Sprite should be selecting a variable, but I couldn't find any variable that can fit in this field. I've tried all variable types and none worked, the drop box always show "none".

I need a way to change the sprite using the sprite name generated at runtime.

Thanks,

Alexandre

TheBadFeeling

  • Junior Playmaker
  • **
  • Posts: 66
Re: Can't change a Sprite Renderer sprite at runtime
« Reply #1 on: September 27, 2014, 07:18:38 AM »
I'm having the same problem, but found this great action to switch a sprite from inside a State.
http://hutonggames.com/playmakerforum/index.php?topic=7797.msg37521#msg37521

My only issue now is that I can't assign a sprite dynamically, only drag them into this Action manually. I need to switch the sprite based on a sprite name I build from variables.

Var1 = "up"
Var2 = "left"
Assign sprite "Head"+Var1+Var2

Anyone else got any ideas how to achieve this?
The Force is with you, young Playmaker – but you are not a C# senpai yet.

TrentSterling

  • Junior Playmaker
  • **
  • Posts: 89
  • Someday I'll make games!
    • My Blog
Re: Can't change a Sprite Renderer sprite at runtime
« Reply #2 on: September 27, 2014, 12:50:38 PM »
Single Sprite:
Code: [Select]
SpriteRenderer.sprite = Resources.Load<Sprite>("Sprites/MySprite");
Sprite Atlas:
Code: [Select]
Sprite[] sprites = Resources.LoadAll<Sprite>("Sprites/MySpriteContainer") as Sprite[]
Turn those into actions.  8)

TheBadFeeling

  • Junior Playmaker
  • **
  • Posts: 66
Re: Can't change a Sprite Renderer sprite at runtime
« Reply #3 on: September 28, 2014, 08:55:41 AM »
Thanks, but since I'm not a programmer, I wouldn't have a clue what to do with these... Could you be more specific?
The Force is with you, young Playmaker – but you are not a C# senpai yet.

TrentSterling

  • Junior Playmaker
  • **
  • Posts: 89
  • Someday I'll make games!
    • My Blog
Re: Can't change a Sprite Renderer sprite at runtime
« Reply #4 on: September 30, 2014, 05:50:06 PM »
Unity loads only the assets referenced in the project. It skips images that aren't needed/used. That means you can't load a bunch of sprites without adding them to an array or something in code.

Assets put in the resources folder get loaded into the build no matter what.

Resources.LoadAll('path') loads all sprites/images/sounds in the resource path you specify.

Hopefully someone cool will come across this thread and turn those snippets into actions, as that's what's needed to load sprites dynamically with a string.

TheBadFeeling

  • Junior Playmaker
  • **
  • Posts: 66
Re: Can't change a Sprite Renderer sprite at runtime
« Reply #5 on: October 04, 2014, 03:10:16 AM »
Thanks for your input. Fortunately, a friend was able to help me setting a script up that I could call from PlayMaker, so I'm up and running now.
The Force is with you, young Playmaker – but you are not a C# senpai yet.

nighty9

  • Playmaker Newbie
  • *
  • Posts: 18
Re: Can't change a Sprite Renderer sprite at runtime
« Reply #6 on: December 10, 2014, 12:37:04 AM »
Hi :)

It would be great if you could share that script :)

Basically i would like when i instantiate a prefab, to set it's sprite based on that sprite's name.

Ex: I load UnitPrefab and set it to Unit1, i would like to set it's sprite renderer property to Unit1Sprite ( Unit1Sprite is in my sprites folder )

Any ideas how to achieve that, or if that needs a script?

Cheers,
Nicolas