2012-03-28 53 views
0

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

但是,你只是不能使用添加添加新元素到配料数组(也试图给两个数组相同的变量),或者只是在最后添加一个新的元素,并单独填充元素。

脚本解锁成分:

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; 

} 

有没有办法从解锁阵列添加一个新的名称和图标,以玩家配料阵列?

在此先感谢!

+0

“但是,你只是不能使用添加添加新的元素到配料数组(也试图给两个数组相同的变量),或者只是在最后添加一个新的元素,并分别填充元素。 “为什么不? – Polyfun 2012-03-28 13:51:40

+1

数组是不可变的,如果你想要一个增长的数据结构,可以使用List。 – MrFox 2012-03-28 13:52:26

+0

所以,你说'ingUnlock.Add(new ingUnlocks());'不起作用 – 2012-03-28 13:53:56

回答

1

很难确切地告诉你以后在做什么,但是这并回答你的问题?:

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

PS:我刚刚完成的代码从你的样品,不会编译线,并增加了新的配料项目,猜测这是你想要做的。

+0

是的!这就像一个魅力。我不知道如何为变量数组添加多个引用,但现在我知道了!非常感谢!谢谢大家的帮助和评论。 – Amacoder 2012-03-28 19:23:08