Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Simple way to change sprites with animations?

Discussion in '2D' started by mike_97, Jan 15, 2021.

  1. mike_97

    mike_97

    Joined:
    Jan 24, 2020
    Posts:
    9
    I have a player sprite with its own 2d animation. But what I would like to do is that everytime you click on the sprite it changes to the next sprite or prefab? I have 4 sprites in total all of them as a prefab Except for the starting sprite! So once you cycle through the sprites it goes back to the beginning sprite again! I've tried using a variable method but if I'm being honest I dont seem to understand c# very well!
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Hello @mike_97,
    Could you share your current implementation, so we have some base to start from?
    Thanks!
     
  3. mike_97

    mike_97

    Joined:
    Jan 24, 2020
    Posts:
    9
    Well I have 4 scenes and 2 of them are used with the player the first scene is the main menu where you would choose the sprite by clicking on the player sprite. The game is a 2d flappy bird style game theres 4 sprites with their own animators!
     
  4. mike_97

    mike_97

    Joined:
    Jan 24, 2020
    Posts:
    9
    Hey I figured It out!
     
  5. mike_97

    mike_97

    Joined:
    Jan 24, 2020
    Posts:
    9
    I figured it out but now there's another problem the sprite that I choose from revert to the default sprite when I switch the scene! Like in the pictures.
     
  6. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Great that you figured out your issue.
    I don't quite understand your new issue though, could you elaborate?
     
  7. mike_97

    mike_97

    Joined:
    Jan 24, 2020
    Posts:
    9
    Well I can pick out of 4 sprites in my starting menu scene, but when I transition to my game scene its back to the first sprite.
     
  8. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Without any code showing what you are doing, it is very tricky to narrow down what goes wrong
     
  9. mike_97

    mike_97

    Joined:
    Jan 24, 2020
    Posts:
    9
    Here is the code I use for changing sprites.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class Sprites : MonoBehaviour
    {
    private Animator anim;
    float setsprite = 0f;
    // Start is called before the first frame update
    void Start()
    {
    anim = GetComponent<Animator>();
    }

    void Update()
    {
    if (setsprite == 4)
    {
    setsprite = 0;
    }
    anim.SetFloat("Trutet", setsprite);
    }

    // Update is called once per frame
    void OnMouseDown()
    {
    setsprite += 1;
    }
    }
     
  10. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    If you are using
    SceneManager.LoadScene();
    to switch scene with the default call, the current scene is unloaded and the next scene is then loaded in. This means that the objects that lived in the first scene are destroyed and their data does not exist anymore.
    There are different ways to attack this problem, but I would suggest you to have a look at one of our beginner tutorials to get a good base understanding on how to work with Unity. This, and a lot of other pitfalls are covered in these tutorials.
    https://learn.unity.com/