在数组中设置对象属性C#

时间:2016-06-24 21:44:52

标签: c# arrays list object properties

我正在为我在Unity引擎中工作的游戏创建用户帐户。我认为实现这一目标的最佳方法是将一个对象添加到ArrayList中(如果我没有记错,使用ArrayList则不需要定义Array的大小)。我设置了对象属性(我希望每个新玩家拥有的基本属性)。当我到达accessInfo代码段中的foreach循环时,我试图引用对象的特定属性(例如,设置为30的playerAgility,用于演示目的)。任何帮助都会很棒。谢谢!

newProfile脚本:

    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();
    }
} 

个人资料脚本(示例不是为了创建大量帖子):

    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; }
    }
}

调试类:

    public class debug : MonoBehaviour
{
    void Start()
    {
        new newProfile("working");
    }     
}

1 个答案:

答案 0 :(得分:0)

Trevor Ward提供的解决方案:

"在foreach循环中,您将使用element.profileStamina访问stamina属性(例如)。 "