Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向变量数组添加新变量_C#_Arrays_Variables_Unity3d - Fatal编程技术网

C# 向变量数组添加新变量

C# 向变量数组添加新变量,c#,arrays,variables,unity3d,C#,Arrays,Variables,Unity3d,我有一个包含多个变量(如名称、图标、值等)的成分数组。当玩家超过某些点数时,必须向该配料数组添加新配料。因此,我还有一个数组,其中包含新成分的图标和名称(值将在运行时单独添加) 但问题是,您不能使用Add将新元素添加到配料数组(也尝试为两个数组提供相同的变量),或者只在末尾添加新元素并分别填充元素 解锁成分的脚本: using UnityEngine; using System.Collections; using System.Collections.Generic; public clas

我有一个包含多个变量(如名称、图标、值等)的成分数组。当玩家超过某些点数时,必须向该配料数组添加新配料。因此,我还有一个数组,其中包含新成分的图标和名称(值将在运行时单独添加)


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

解锁成分的脚本:

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;
    
}
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类progressManager:MonoBehavior{
私密的国际联络级别;
私有内部锁级别=0;
私有int newIngScore=500;
public List ingUnlock=新列表();
public List lockedCraft=新列表();
公共纹理锁;
公共纹理正常;
私有布尔升级;
公共TextMesh NextingCredit;
//每帧调用一次更新
无效更新(){
nextIngredient.text=ingUnlock[lockLevel]。名称;
ingUnlockLevel=GetComponent().磁头芯;
如果(ingUnlockLevel>=newIngScore){upGrade=true;}
如果(升级==true){
GetComponent().成分。;
newIngScore=newIngScore*2;
锁级别+=1;
升级=假;
}
}
[系统可序列化]
英格兰洛克公务舱{
公共字符串名称;
公共纹理图标;
}
}
和部分剧本中的球员成分:

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;
    
}
使用UnityEngine;
使用制度;
使用System.Linq;
使用系统集合;
使用System.Collections.Generic;
公共类productManager:MonoBehavior{
私有类IngCreditComparer:IComparer{
公共整数比较(成分a、成分b)
{
返回((int)a.c2cPerc)-(int)b.c2cPerc);
}
}
公共列表成分=新列表();
}
[系统可序列化]
公共级配料{
公共字符串名称;
公共纹理图标;
公众智力得分;
公共整数;
公共交通委员会;
公共图书馆可用;
}

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

很难说清楚你到底想要什么,但这能回答你的问题吗

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

PS:我刚刚从您的示例中完成了一行代码,它不会编译并在那里添加一个新的配料项目,我猜这就是您想要做的。

“但问题是您不能使用Add向配料数组添加新元素(还尝试为两个数组提供相同的变量)“为什么不呢?数组是不可变的,如果您想要一个增长的数据结构,请使用一个列表。因此,您说的是
ingUnlock.add(new ingUnlock())不存在work@MrFox但是示例代码显示的是一个列表,而不是一个数组。Kevin,您的示例代码没有声明单个数组。所以我被你的问题弄糊涂了。是的!这很有魅力。我不知道如何向变量数组添加多个引用,但现在我知道了!非常感谢你!谢谢大家的帮助和评论。