Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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_Unity3d - Fatal编程技术网

C# 数组数据每隔一帧就有意外数据

C# 数组数据每隔一帧就有意外数据,c#,arrays,unity3d,C#,Arrays,Unity3d,控制台日志 我的列表添加精灵名称并将其转换为整数以添加到列表中,然后我创建了一个函数来显示列表,但每当我在更新循环中显示列表时,它都会交替显示0 和实际值。当我不把它放在更新循环中时,它只显示0 public class ballMove : MonoBehaviour { int[] BallOrder = new int[5]; // me initializing the variable in the beginning of the program // int[] BallO

控制台日志

我的列表添加精灵名称并将其转换为整数以添加到列表中,然后我创建了一个函数来显示列表,但每当我在更新循环中显示列表时,它都会交替显示0

和实际值。当我不把它放在更新循环中时,它只显示0

public class ballMove : MonoBehaviour {

int[] BallOrder = new int[5]; // me initializing the variable in the 
beginning of the program

// int[] BallOrder = {4,6,3,2,7}; if I do this instead of showing 0's it 
shows 4,6,3,2,7 when I click the mouse down.

void CreateBalls(int HowMany) {
// makes balls with a unique picture size and position
for (int i = 0; i < HowMany; i++) {
        BallClone = Instantiate(Ball);

        BallPic = BallClone.GetComponent<SpriteRenderer>();
        // give each ball a random image, each image is a number
        RandImg(); 
        // just using i for testing purposes.        
        BallOrder[i] = i;
        //array should just be 1,2,3,4,5
    }
}

void ShowList() 
{
    System.Array.Sort(BallOrder);
    string text = "";
    
    for (int i = 0; i < BallOrder.Length; i++) 
    {
        text = (text + " " + BallOrder[i]);
    }
    
    print(text);
}

void Update()
{
    // here it shows the array correctly 1,2,3,4,5
    ShowList();
}
void OnMouseDown() {
    string BallNum = ConvertSprite(GetComponent<SpriteRenderer>().sprite);
    int test = int.Parse(BallNum);
    ShowList(); // here when I show list it just shows 0,0,0,0,0
    Destroy(gameObject);
}

}
public class ballMove:monobhavior{
int[]BallOrder=new int[5];//我正在初始化
节目开始
//int[]BallOrder={4,6,3,2,7};如果我这样做而不是显示0的
单击鼠标时显示4,6,3,2,7。
void CreateBalls(int有多少个){
//使球具有独特的图片大小和位置
for(int i=0;i<多少;i++){
BallClone=实例化(Ball);
BallPic=BallClone.GetComponent();
//给每个球一个随机图像,每个图像都是一个数字
RandImg();
//仅使用i进行测试。
BallOrder[i]=i;
//数组应仅为1,2,3,4,5
}
}
void ShowList()
{
System.Array.Sort(BallOrder);
字符串文本=”;
for(int i=0;i
为了回答评论中没有的问题,问题是控制台打印出了可能被认为是坏数据的内容

通过在预期的(6个随机数)和不预期的(6个零)之间交替,显然,函数被调用了两次,但从进一步添加的代码片段来看,没有理由解释为什么函数会被多次调用

从数组数据的初始化来看,只有当数据被覆盖或从未初始化时,才会出现6个零而不是数据。由于数组在初始化后再也不会写入,这意味着它的初始化不正确

发生这种情况的唯一方法是将脚本附加到场景中的两个对象,其中一个对象正在正确初始化数据,而另一个对象在
Start
中没有初始化


解决方案是从场景中的两个对象之一删除未使用的脚本。

是否还有其他地方可以调用
ShowList()
?解析
PicName
以获取添加到此列表中的整数的频率和位置?从代码上看,你贴的很难说。我相信在某个地方,您正在用所有0覆盖现有列表。能否为设置数组的方式和位置添加更多的上下文?我唯一使用showList的时间是在更新循环中。我将编辑我的帖子,添加更多的内容,希望能有所帮助。顺便说一句,这是我第一次使用这个,所以我不会在这个专业。基本上我把精灵图像转换成一个字符串,然后把字符串转换成一个整数,然后添加到列表中。一般来说,所有代码都应该打印出来并放在问题中,而不是以图像的形式发送。由于它打印了两次,我猜您的项目中有两个地方有这个脚本,要么在同一个游戏对象上,要么在不同的对象上。我的另一个想法是,你是如何抓住名字的子串的,是在做一些不可靠的事情。行
test.Substring(0,2)
,是否所有名称编号都至少有这么长?还是短一点?我把它从脚本中分离出来,它显示正确,谢谢你的帮助,伙计:)。