Brian Martin
Brian Martin

Reputation: 3

C# debug.log not displaying correct info in method.(using mono and unity)

I'm taking on the task of designing a game in unity. I'm not an expert at programming, but I have a fare share of knowledge in Ansi C. So forgive me if my code is C# like.

The method works just fine but for some strange reason the Debug.Log(string.Format("",) displays:

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

It displays the i variable and pop_class[i] if i simply type debug.log with the same information outside of method everything displays correctly. Any idea why this is the case? The program is complex enough i don't want to have to wrestle with log files while i'm debugging.

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;
}

Upvotes: 0

Views: 1462

Answers (1)

Steve
Steve

Reputation: 6424

Change this line:

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

to this:

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

You're printing out the first parameter twice, so pop_class[i] is never being printed. You need to change {0} to {1}.

The String.Format() function accepts an array of objects as its second parameter (at least in this case). This allows you to specify any number of arguments to the function. {0} tells Format() to output the second parameter to the function, {1} the third, etc. The first parameter is the format string.

Upvotes: 1

Related Questions