向变量数组添加新变量

时间:2012-03-28 13:40:53

标签: c# arrays variables unity3d

我有这个成分数组包含多个变量(如名称,图标,值等)。当玩家超过某些点时,必须将新成分添加到该成分阵列中。所以我有一个其他数组,其中包含新成分的图标和名称(值将在运行时单独添加)。

但问题是你不能使用Add将新元素添加到components数组中(也尝试给两个数组提供相同的变量),或者只是在末尾添加一个新元素并分别填充元素。

解锁成分的脚本:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class progressManager : MonoBehaviour {

    private int ingUnlockLevel;
    private int lockLevel = 0;
    private int newIngScore = 500;
    public List<ingUnlocks> ingUnlock = new List<ingUnlocks>();
    public List<GameObject> lockedCraft = new List<GameObject>();
    public Texture locks;
    public Texture normal;
    private bool upGrade;

    public TextMesh nextIngredient;
    // Update is called once per frame
    void Update () {

        nextIngredient.text = ingUnlock[lockLevel].name;

        ingUnlockLevel = GetComponent<gameMechanics>().headScore;

        if(ingUnlockLevel >= newIngScore){upGrade = true;}

        if(upGrade == true){    
            GetComponent<productManager>().ingredient.;
            newIngScore = newIngScore *2;
            lockLevel+=1; 
            upGrade = false;
        }
    }

    [System.Serializable]
    public class ingUnlocks{
        public string name;
        public Texture icon;
    }
}

以及球员成分的部分脚本:

using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class productManager : MonoBehaviour {

    private class ingredientComparer : IComparer<ingredients>{

        public int Compare(ingredients a, ingredients b)
        {
            return ((int) a.c2cPerc ) - ((int) b.c2cPerc);
        }
    }

        public List<ingredients> ingredient = new List<ingredients>();
}

[System.Serializable]
public class ingredients{

    public string name;
    public Texture icon;
    public int score;
    public int quantity;
    public float c2cPerc;
    public bool usable;

}

有没有办法将解锁阵列中的新名称和图标添加到玩家成分阵列?

提前致谢!

1 个答案:

答案 0 :(得分:1)

很难说出你到底发生了什么,但是这回答了你的问题吗?:

GetComponent<productManager>().ingredient.Add(new ingredients { name = ingUnlock[locklevel].name, icon = ingUnlock[locklevel].icon });

PS:我刚刚完成了你的样本中不能编译的代码行,并在那里添加了一个新的成分项,猜测这就是你想要做的。