Search Unity

Changing SpriteRenderer between Scenes

Discussion in 'Scripting' started by Shinsuki93, Nov 27, 2017.

  1. Shinsuki93

    Shinsuki93

    Joined:
    Aug 11, 2017
    Posts:
    38
    Hi,

    I'm doing a game where the player can switch the Character.
    But I'll do this with the SpriteRenderer.

    Anyone can help me with that ?

    I'm trying to do like a MarketPlace with all the characters and when you select one the Sprite Renderer change in all Scene.

    Thanks you very much for your time and help.

    Best regards,
    Shinsuki
     
  2. storylineteam

    storylineteam

    Joined:
    Oct 22, 2015
    Posts:
    24
    Can you be more specific?
    a general rule is to separate classes that contain data from classes that actually displays stuff

    Code (CSharp):
    1. [Serializable]
    2. public class CharIcon
    3. {
    4.     public int id;
    5.     public Sprite sprite;
    6. }
    7. public class CharIconsList : ScriptableObject
    8. {
    9.     public List<CharIcon> CharList;
    10.  
    11.     public static int TheSelectedOneId;
    12.  
    13.     public Sprite GenericPlaceholder;
    14.  
    15.     public Sprite ReturnSelectedOne()
    16.     {
    17.         for (int i = 0; i < CharList.Count; i++)
    18.         {
    19.             if (CharList[i].id == TheSelectedOneId)
    20.             {
    21.                 return CharList[i].sprite;
    22.             }
    23.         }
    24. //////////
    25.  
     
    Last edited: Nov 27, 2017
  3. Shinsuki93

    Shinsuki93

    Joined:
    Aug 11, 2017
    Posts:
    38

    Hello,

    Thanks you for your response.

    For the game I have a Ball Green and Yellow with Rigibody, Script, circle collider.
    So, in my character Shop if the player want for example a Ball "Batman" I change just the Sprite Renderer.
    But When I change the Sprite Renderer He don't save my modification in my others Scene..

    Maybe it is my way to do that, it's not good ?

    Here is my code :

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class SelectionCharacter : MonoBehaviour {
    8.  
    9.     public Sprite Blue, Batman;
    10.     // Use this for initialization
    11.     void Start () {
    12.        
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.        
    18.     }
    19.  
    20.     public void ToggleRight()
    21.     {
    22.         this.GetComponent<SpriteRenderer> ().sprite = Batman;
    23.     }
    24.  
    25.     public void ToggleLeft()
    26.     {
    27.         this.GetComponent<SpriteRenderer> ().sprite = Blue;
    28.     }
    29.  
    30.     public void ConfirmButton()
    31.     {
    32.         SceneManager.LoadScene("FirstScene");
    33.     }
    34. }
    35.  
    36.  
    Thanks you for you're help :)

    Best regards,
    Shinsuki