0

I am working on user account creation for a game I am working on in the Unity engine. I figured the best way to achieve this would be to have an object added to an ArrayList (if I am not mistaken, with an ArrayList you do not need to define the size of the Array). I set the object properties (the base stats that I want every new player to have). When I get to the foreach loop in the accessInfo segment of code, I get stuck trying to reference a particular property of the object (for instance, playerAgility which is set to 30, for demonstration purposes). Any help would be great. Thanks!

newProfile script:

    using UnityEngine;
    using System.Collections;

    public class newProfile : Profile
{
    ArrayList newprofile = new ArrayList();

    public void newplayer(string input)
    {
        newprofile.Add(new Profile(10, 20, 30, 40, 50, input));
    }

    public void accessInfo()
    {

        foreach (Profile element in newprofile) 
        {

           // Debug.Log(need to access the values set for the profile);
        }

    }

    public newProfile(string input)
    {
        newplayer(input);
        accessInfo();
    }
} 

Profile script (A sample as not to create a massive post):

    public class Profile
{
        private Profile user;
        private int PROFILE_STAMINA, PROFILE_STRENGTH, PROFILE_AGILITY;
        private int PROFILE_INTELLECT, PROFILE_SKILL,PROFILE_MASTERY;
        private string ProfileName;

    public Profile(int Stamina, int Strength, int Agility, int Intellect, int Skill, string username)
    {
        user = new Profile();
        profileStamina = Stamina;
        profileStrength = Strength;
        profileAgility = Agility;
        profileIntellect = Intellect;
        profileSkill = Skill;
        userName = username;
    }

    public int profileStamina
    {
        get { return PROFILE_STAMINA; }
        set { PROFILE_STAMINA = value; }
    }

    //Identical setters and getters for the remaining stats

    public string userName
    {
        get { return ProfileName; }
        set { ProfileName = value; }
    }
}

Debug class:

    public class debug : MonoBehaviour
{
    void Start()
    {
        new newProfile("working");
    }     
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    Is there a reason why you're using `ArrayList` instead of `List`? – David L Jun 24 '16 at 21:50
  • I am very new to this subject, so I am not 100% sure of the difference, other than that a List is generic compared to an ArrayList which stores object references. Correct me if I am wrong :) – Timothy White Jun 24 '16 at 21:54
  • 1
    Here's a good answer regarding ArrayList vs List. The key takeaway is this: "ArrayList belongs to the days that C# didn't have generics. It's deprecated in favor of List" http://stackoverflow.com/a/2309699/4689483 – JTaub Jun 24 '16 at 22:00
  • Okay so I will change my script to use a List, but then how do I access the information stored in there (such as the playerStamina that I set as 10, or the playerAgility set as 30, etc).Will making this a List fix my issue? Thanks in advance! – Timothy White Jun 24 '16 at 22:03
  • Maybe I don't understand what your problem is... but in the foreach loop you would access the stamina property (for instance) using element.profileStamina. – Trevor Ward Jun 24 '16 at 22:29
  • Well ya fixed that for me. I didn't realize I needed the word element before profileStamina, so it wasn't working. Thanks! – Timothy White Jun 25 '16 at 00:26
  • I rolled back your edit, because it's inappropriate to add [SOLVED] to your title. If you've found a solution from a comment, ask the poster to put it in the form of an answer, which you can accept (and upvote if you'd like) to show it's solved your problem. If that user doesn't want to post an answer, you can feel free to post one yourself showing the solution, **in the space below titled *Your Answer*** (see [help/self-answer] for details). If neither are suitable to you, feel free to delete your question, because without a solution it is useless to future readers here. – Ken White Jun 25 '16 at 00:48

1 Answers1

0

Solution provided by Trevor Ward:

" In the foreach loop you would access the stamina property (for instance) using element.profileStamina. "