C#debug.log没有在方法中显示正确的信息。(使用单声道和统一)

时间:2013-07-01 17:11:30

标签: c# debugging methods unity3d monodevelop

我正在承担统一设计游戏的任务。我不是编程方面的专家,但我在Ansi C中有一定的知识分享。如果我的代码是C#,请原谅我。

该方法效果很好,但由于某些奇怪的原因Debug.Log(string.Format("",)显示:

pop_class index value 0 value = 0,
pop_class index value 1 value = 1,   

它显示了i变量和pop_class [i],如果我只是键入debug.log,并且方法之外的所有信息都正确显示。 知道为什么会出现这种情况吗?该程序非常复杂,我不想在调试时与日志文件搏斗。

public int pop_generation() //debugger is currently not displaying vars correctly
{   
    int pop_increase = 1000000;
    int pop_counter = 0;//counts populations assigned by the 1000s
    int high_class_per = 10;
    int mid_class_per = 50;
    int low_class_per = 40;
    int lpoptotal = (faction_pop/100) * low_class_per;
    int lpop_count = 0;
    int mpoptotal = (faction_pop/100) * mid_class_per;
    int hpoptotal = (faction_pop/100) * high_class_per;
    //calucates certain percentage of low class
    int mid_class_pop = (faction_pop/100) * mid_class_per;
    int high_class_pop = (faction_pop/100) * high_class_per;
    System.Random random_now = new System.Random();
    for(int i = 0; i < replimit_check && pop_counter < faction_pop;i++)
    {
        //keeps total of low class rep population by 1,000s
        if(lpop_count > lpoptotal)
        {
            Debug.Log("Low class generation complete");
            pop_class[i] = 0;
            display_high_class();
            i--;//brings incrementer back once
            break;
        }
        pop_class[i] = random_now.Next(20);
        pop_counter = pop_increase + pop_counter;
        lpop_count = pop_increase + lpop_count;
        if(debug_on)
            Debug.Log(string.Format("Pop_class index value {0} value = {0} ",i,pop_class[i])); //debug.log is bugged

    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

更改此行:

Debug.Log(string.Format("Pop_class index value {0} value = {0} ",i,pop_class[i])); //debug.log is bugged

到此:

Debug.Log(string.Format("Pop_class index value {0} value = {1} ",i,pop_class[i])); //debug.log is bugged

您打印出第一个参数两次,因此永远不会打印pop_class[i]。您需要将{0}更改为{1}

String.Format()函数接受一个对象数组作为其第二个参数(至少在这种情况下)。这允许您为函数指定任意数量的参数。 {0}告诉Format()将第二个参数输出到函数,{1}输出第三个,等等。第一个参数是格式字符串。