Forum rules - please read before posting.

Change sprite randomly when using hotspot

Hi!

I wonder if I can change a sprite randomly from a collection of sprites whenever I click on it. I have managed to change it once when clicked with the help of the object animate action in the hotspot use object.

Thanks in advance!

Comments

  • Welcome to the community, @StefanieJ.

    Sometimes, it's best to rely on customs scripting for something like this.

    Here's a script you can attach to your Sprite Renderer that has a function to change it's sprite randomly from a collection:

    using UnityEngine;
    
    public class ChangeRandomSprite : MonoBehaviour
    {
    
        public Sprite[] sprites;
        public SpriteRenderer spriteRenderer;
    
        public void ChangeSprite ()
        {
            int randomIndex = Random.Range (0, sprites.Length);
            spriteRenderer.sprite = sprites[randomIndex];
        }
    
    }
    

    To trigger it when using a Hotspot, have your Hotspot Interaction run an Object: Call event Action that references this component's ChangeSprite function.

  • Thank you, Chris!

    Still, I know only the basics of scripting and attaching scripts. I tried and got some errors. (Can I upload images here?)

    Is it right to add the script as a component of the sprite? Then again it is only one of the sprites. How does the script know from the other sprites?

  • I have the other sprites assigned now and there are no errors anymore. But the Call event does not work for the sprite change, yet. Do I have to assign any function (there are various functions and I have no idea which one it could be.)

  • You need to call the component's ChangeSprite function.

    Make sure that the script is added as a component on the same GameObject as your SpriteRenderer, and that you've assigned that SpriteRenderer (as well as the Sprites) in its Inspector.

    When using the Object: Call event Action, you must be sure to reference the ChangeSprite function in the component - not the original script file. Do this by first clicking the + icon in the Action, then dragging the GameObject with the script/SpriteRenderer into the field. From the drop down that then appears beside it, you can then choose ChangeRandomSprite -> ChangeSprite.

    If you have problems, take screenshots, post them to imgur.com, and paste links in a reply.

  • edited April 2021

    Thank you, but I have not managed, yet. I might go back to some basics and learn or just solve it over time.

    Then again, there is a warning shown in the node:
    Parameters past from here cannot be set, unfortunately, due to a Unity limitation.

    Maybe it has nothing to do with my problem. I am just curious what this means.

  • The warning refers to being unable to pass parameters to the function the Action calls.

    The script I've given you has no parameters, so you can safely ignore the warning - it's just there as general information, unrelated to what you've actually assigned in the Action's UI.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.