C#debug.log没有在方法中显示正确的信息.(使用mono和unity)
本文关键字:信息 使用 mono unity log 方法 显示 C#debug | 更新日期: 2023-09-27 18:20:31
我正在承担统一设计游戏的任务。我不是编程专家,但我在Ansi C中有很多知识。所以,如果我的代码像C#,请原谅我。
该方法运行良好,但由于一些奇怪的原因,Debug.Log(string.Format("",)
显示:
pop_class index value 0 value = 0,
pop_class index value 1 value = 1,
如果我只是在方法之外键入debug.log并使用相同的信息,那么它将显示i变量和pop_class[i],所有内容都会正确显示<知道为什么会这样吗>这个程序很复杂,我不想在调试时与日志文件纠缠。知道为什么会这样吗>
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;
}
更改此行:
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}
输出第三个参数,等等。第一个参数是格式字符串。