Search Unity

help with character not changing

Discussion in 'Scripting' started by jbowers74321, Jan 21, 2020.

  1. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    i have a character select scene and when i change my character and hit play my character is still the default character here is my CharacterSwitcher code
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class CharacterSwitcher : MonoBehaviour
    5. {
    6.     private void Awake()
    7.     {
    8.         if (currentIndex != -1)
    9.             return;
    10.         SetCharacter(0);
    11.         currentIndex = 0;
    12.         Debug.Log("CharacterSwitcher awake");
    13.     }
    14.  
    15.     [SerializeField] private Transform playerRoot;
    16.  
    17.     [SerializeField] public static int currentIndex = -1;
    18.  
    19.     public void NextCharacter()
    20.     {
    21.         //Add 1 to our current index
    22.         currentIndex++;
    23.         //Make sure it doesn't go too high
    24.         if (currentIndex >= playerRoot.childCount)
    25.             currentIndex = 0;
    26.         SetCharacter(currentIndex);
    27.     }
    28.  
    29.     public void SetCharacter(int transformIndex)
    30.     {
    31.         //Disable all our child transforms
    32.         for (int i = 0; i < playerRoot.childCount; i++)
    33.             playerRoot.GetChild(i).gameObject.SetActive(false);
    34.         //Enable only the one we want
    35.         playerRoot.GetChild(transformIndex).gameObject.SetActive(true);
    36.        
    37.     }
    38.  
    39. }
    and I attached public void NextCharacter to my button and in the character select scene it changes but when I hit play it is still the default character. I also added this line to my play button to try to save the character decision I made to play as
    Code (CSharp):
    1. UnityEngine.Object.FindObjectOfType<CharacterSwitcher>().SetCharacter(CharacterSwitcher.currentIndex);
    I added a debug log to the awake function and got nothing I also set up breakpoints in visual studio in the awake function and when I select a character and hit a back button to go to a main menu scene and then hit play I got this error Exception thrown at 0x00000000425594F9 in Unity.exe: 0xC0000005: Access violation reading location 0x0000000000000000 but if I select a character then hit play in the character select scene it's ok and doesn't throw any errors.
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Is the index being saved anywhere? As soon as another scene loads, the index isn't retained unless stored in a DontDestroyOnLoad object or to a file that is read when the play scene is loaded.
     
  3. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I thought the line UnityEngine.Object.FindObjectOfType<CharacterSwitcher>().SetCharacter(CharacterSwitcher.currentIndex); would retain what i chose throughout scenes how can i fix this, what code do i need to add?
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Nothing is retained between scenes unless you tell it to. Take a look at DontDestroyOnLoad in the docs to see how. There are also many ways to save a file, do a search for a tutorial on it to get an idea of what to do so you can save game play progress.