Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make an RPG YouTube Series

Discussion in 'Scripting' started by Krory, Jun 18, 2016.

  1. Krory

    Krory

    Joined:
    Jun 18, 2016
    Posts:
    7
    I've been following this tutorial series :


    So far this is what my script looks like for DisplayCreatePlayerFunctions
    using UnityEngine;
    using System.Collections;

    public class DisplayCreatePlayerFunctions {

    private int classSelection;
    private string[] classSelectionNames = new string[] { "Mage", "Warrior", "Archer", "Thief"};

    public void DisplayClassSelections()
    {
    classSelection = GUI.SelectionGrid(new Rect(50,50,250,300), classSelection, classSelectionNames, 2);

    }

    private string FindClassDescription(int classSelection)
    {
    if (classSelection == 0)
    {
    BaseCharacterClass tempClass = new BaseMageClass();
    return tempClass.CharacterClassDescription;
    }
    }

    public void DisplayStatAllocation()
    {

    }

    public void DisplayFinalSetup()
    {

    }
    public void DisplayMainItems()
    {
    GUI.Label(new Rect(Screen.width / 2, 20, 250, 100), "CREATE NEW PLAYER");
    }
    }

    The bold text is whats giving me this error: "
    Assets/Scripts/CreateCharacterGUI/DisplayCreatePlayerFunctions.cs(15,20): error CS0161: `DisplayCreatePlayerFunctions.FindClassDescription(int)': not all code paths return a value"
     
    Last edited: Jun 20, 2016
  2. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    You want "stamina = value", not "Stamina = value", and "return stamina", not "return Stamina"
    This is what is causing the stack overflow, as your script keeps trying to get Stamina, which in turn tries to get Stamina, leading to a run away infinite loop.

    You have the same problem with every attribute, you want to set/get the value of the variable that the setter and getter is responsible for.
    Example:

    Code (CSharp):
    1. public class SomeClass {
    2.  
    3. private int _health;
    4. // This setter and getter allows outside scripts to access the _health variable.
    5. public Int Health{
    6. get {return _health;}
    7. set{_health = value;}
    8. }
    9.  
    10. }
     
    Krory likes this.
  3. Krory

    Krory

    Joined:
    Jun 18, 2016
    Posts:
    7
    @TonanBora Thanks! I was able to get past all that, but now I'm on episode 12 (Saving/Loading)
    From what I can tell I have all the scripts right but it doesn't load the proper level or stats, however it does save my name correctly.

    using UnityEngine;
    using System.Collections;

    public class TsetScript : MonoBehaviour {

    // Use this for initialization
    void Start ()
    {
    LoadInformation.LoadAllInformation();

    Debug.Log("Player Name:" + GameInformation.PlayerName);
    Debug.Log("Player Level:" + GameInformation.PlayerLevel);
    Debug.Log("Player Stamina:" + GameInformation.Stamina);
    Debug.Log("Player Strength:" + GameInformation.Strength);
    Debug.Log("Player Intellect:" + GameInformation.Intellect);
    Debug.Log("Player Endurance:" + GameInformation.Endurance);
    }

    // Update is called once per frame
    void Update () {

    }
    }

    using UnityEngine;
    using System.Collections;

    public class SaveInformation {
    public static void SaveAllInformation()
    {
    PlayerPrefs.SetInt("PLAYERLEVEL", GameInformation.PlayerLevel);
    PlayerPrefs.SetString("PLAYERNAME", GameInformation.PlayerName);
    PlayerPrefs.SetInt("STAMINA", GameInformation.Stamina);
    PlayerPrefs.SetInt("STRENGTH", GameInformation.Strength);
    PlayerPrefs.SetInt("ENDURANCE", GameInformation.Endurance);
    PlayerPrefs.SetInt("INTELLECT", GameInformation.Intellect);
    Debug.Log("SAVED ALL INFORMATION");
    }
    }

    using UnityEngine;
    using System.Collections;

    public class LoadInformation {
    public static void LoadAllInformation()
    {
    GameInformation.PlayerName = PlayerPrefs.GetString("PLAYERNAME");
    GameInformation.PlayerLevel = PlayerPrefs.GetInt("PLAYERLEVEL");
    GameInformation.PlayerLevel = PlayerPrefs.GetInt("STAMINA");
    GameInformation.PlayerLevel = PlayerPrefs.GetInt("ENDURANCE");
    GameInformation.PlayerLevel = PlayerPrefs.GetInt("INTELLECT");
    GameInformation.PlayerLevel = PlayerPrefs.GetInt("STRENGTH");
    }
    }

    using UnityEngine;
    using System.Text.RegularExpressions;
    using UnityEngine.SceneManagement;
    public class CreateNewCharacter : MonoBehaviour {

    private BasePlayer newPlayer;
    private bool isMageClass;
    private bool isWarriorClass;
    private string playerName = "Name:";
    // Use this for initialization
    void Start () {
    newPlayer = new BasePlayer();
    }

    // Update is called once per frame
    void Update () {

    }

    void OnGUI()
    {
    playerName = GUILayout.TextField(playerName, 15);
    playerName = Regex.Replace(playerName, @"[^a-zA-Z0-9]", "");
    isMageClass = GUILayout.Toggle(isMageClass, "Mage Class");
    isWarriorClass = GUILayout.Toggle(isWarriorClass, "Warrior Class");
    if (GUILayout.Button("Create"))
    {
    if (isMageClass)
    {
    newPlayer.PlayerClass = new BaseMageClass();
    }
    else if (isWarriorClass)
    {
    newPlayer.PlayerClass = new BaseWarriorClass();
    }
    newPlayer.PlayerLevel = 1;
    newPlayer.Stamina = newPlayer.PlayerClass.Stamina;
    newPlayer.Strength = newPlayer.PlayerClass.Strength;
    newPlayer.Endurance = newPlayer.PlayerClass.Endurance;
    newPlayer.Intellect = newPlayer.PlayerClass.Intellect;
    newPlayer.PlayerName = playerName;
    StoreNewPlayerInfo();
    SaveInformation.SaveAllInformation();

    Debug.Log("Player Class:" + newPlayer.PlayerClass.CharacterClassName);
    Debug.Log("Player Level:" + newPlayer.PlayerLevel);
    Debug.Log("Player Stamina:" + newPlayer.Stamina);
    Debug.Log("Player Endurance:" + newPlayer.Endurance);
    Debug.Log("Player Intellect:" + newPlayer.Intellect);
    Debug.Log("Player Strength:" + newPlayer.Strength);
    Debug.Log("Player Name:" + newPlayer.PlayerName);
    }
    if (GUILayout.Button("Load"))
    {
    SceneManager.LoadScene("tset");
    }
    }
    private void StoreNewPlayerInfo()
    {
    GameInformation.PlayerName = newPlayer.PlayerName;
    GameInformation.PlayerLevel = newPlayer.PlayerLevel;
    GameInformation.Stamina = newPlayer.Stamina;
    GameInformation.Endurance = newPlayer.Endurance;
    GameInformation.Intellect = newPlayer.Intellect;
    GameInformation.Strength = newPlayer.Strength;

    }
    }

    using UnityEngine;
    using System.Collections;

    public class GameInformation : MonoBehaviour {


    void Awake()
    {
    DontDestroyOnLoad(transform.gameObject);
    }
    public static string PlayerName { get; set;}
    public static int PlayerLevel{ get; set; }
    public static BaseCharacterClass PlayerClass{ get; set; }
    public static int Stamina{ get; set; }
    public static int Endurance { get; set; }
    public static int Intellect { get; set; }
    public static int Strength { get; set; }

    }
     
  4. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    For your load script, you keep assigning too GameInformation.PlayerLevel.
    You correctly assign to PlayerName, but the reason all the other info is not getting correctly set, is because your not actually setting them. :p
     
    Krory likes this.
  5. Krory

    Krory

    Joined:
    Jun 18, 2016
    Posts:
    7
    LOL! Thanks again man, I'm running on 3 hours of sleep! :D
     
    TonanBora likes this.
  6. TonanBora

    TonanBora

    Joined:
    Feb 4, 2013
    Posts:
    493
    Not a problem, even experienced programmers forget to do something every now and again. :)
    Now go take a nap! :p
     
  7. Krory

    Krory

    Joined:
    Jun 18, 2016
    Posts:
    7
    @TonanBora Sleep is something I can't do right now, but I do have a few more questions for you.

    If you don't mind I'd like to add you on some form of instant messaging? I keep running into small errors and I could really use the help, and I appreciate how much you've already helped me :)