1

I am looping an array which has 6 values. These values get filled from an external script. How do I check which array[i] has 0 value? Currently the loop shows how many 0 values are there. But does not show which array[i] has 0 value.

public int[] Total_Val;

void Start()
{
  for(int i = 0; i<Total_Val.Length; i++)
             {
                 if(Total_Val[i] <= 0)
                 {
                    Debug.Log("The array" +Total_Val[i]+" has null value");
                 }
             }
}
Tharres
  • 113
  • 7
  • 2
    It's confusing that you use the word `null` here, since `null` has a very special meaning and `0` is definitely _not_ `null`. – ProgrammingLlama Dec 07 '20 at 13:04
  • @John ah sorry then I will change the question. – Tharres Dec 07 '20 at 13:04
  • 2
    I expect all you need to do is also output the value `i` (or `i + 1` if you want positions to start at 1) in your `Debug.Log`. – ProgrammingLlama Dec 07 '20 at 13:05
  • yes ofc. So stupid of me. Also I know null is for empty field. I thought 0 would be considered as null. – Tharres Dec 07 '20 at 13:06
  • 1
    `Debug.Log("The array at " + i + " with the Value " + Total_Val[i]);` – IndieGameDev Dec 07 '20 at 13:07
  • 2
    No. In .NET you have [reference types and value types](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c). Reference types can be `null` (nothing), but value types cannot be `null`. `int` is a value type so it always has a value, even if that value is 0. – ProgrammingLlama Dec 07 '20 at 13:09
  • 2
    _"I thought 0 would be considered as null"_ - that's wrong. And since it's an array of a value type, it's a little more complicated than that. But baseline: `int i = 0` is _not_ considered `null`. Never. A value type var can be "uninitialized" _at most_. But your IDE will probably give you explicit hints regarding that. – Fildor Dec 07 '20 at 13:09
  • 1
    Thank you all for the help. Appreciate it! – Tharres Dec 07 '20 at 13:12
  • 1
    Downvote? Come on guys, give a newbie a break. It's a simple misunderstanding but the question in itself is not _that_ "bad"? – Fildor Dec 07 '20 at 13:15
  • 1
    @Fildor I didn't downvote, but you're right. It's a basic question, but it has everything that a question ought to have: a good problem description, it sets out the expectations, and it includes code that demonstrates the issues. I'll give that an upvote. – ProgrammingLlama Dec 07 '20 at 13:21

2 Answers2

3

If you want to print the indexes of the elements that are 0 then the following should be OK:

Debug.Log($"The array contains a 0 at index {i}");
tymtam
  • 31,798
  • 8
  • 86
  • 126
3

Well you could directly print and also collect these indices:

public int[] Total_Val;

void Start()
{
    var zeroIndices = new List<int>();
    for(var i = 0; i < Total_Val.Length; i++)
    {
        if(Total_Val[i] <= 0)
        {
            Debug.Log($"The array has 0 value at index {i}");
            zeroIndices.Add(i);
        }
    }

    Debug.Log($"All indices with 0 are: {zeroIndices.Count == 0 ? "<None>" : string.Join(",", zeroIndices)}");
}
derHugo
  • 83,094
  • 9
  • 75
  • 115