Basic Question

I have a question… how can you add for(int) values to each other.
I was trying to search in google but couldn’t find anything, so I imagine it like this

int OtherValue;
public void NeighborPixels(Vector2 pos)
    {
        for (int i = -CheckZone; i <= CheckZone; i++)
        {
            for (int j = -CheckZone; j <= CheckZone; j++)
            { 
                 OtherValue += Some.Value + OtherValue;
            }
        }
    }

The question here is unclear. What would Some.Value be?

Like the following for a 2D array?

OtherValue += CheckZone[i, j] + OtherValue;

Like the following for nested arrays?

OtherValue += CheckZone[i][j] + OtherValue;

But as I said - unclear.

This is going to double OtherValue every time, plus add Some.Value.

I doubt that’s what you intend.

Perhaps you mean:

OtherValue += Some.Value;

OR ELSE:

OtherValue = OtherValue + Some.Value;

My goal is to be able to add for ints toghether.
My updated code is this …

    public void NeighborPixels(Vector2 pos)
    {
        for (int i = -CheckZone; i <= CheckZone; i++)
        {
            for (int j = -CheckZone; j <= CheckZone; j++)
            {
                if (generateScript.GetPixel(new Vector2Int(Mathf.RoundToInt(pos.x) + i, Mathf.RoundToInt(pos.y) + j)) != null)
                {
                    var ChosenPixelValue = generateScript.GetPixel(new Vector2Int(Mathf.RoundToInt(pos.x) + i, Mathf.RoundToInt(pos.y) + j)).GetComponent<Pixel>().Value;
                    Debug.Log(ChosenPixelValue); // this gives output of one 1 and zeros which is right however I want them to be combined like total value of those pixels would be 1 because 1 + 0+0+0... is 1
                }
            }
        }
    }

generateScript.GetPixel is this…

    public Pixel GetPixel(Vector2Int pos)
    {
        if(square.TryGetValue(pos, out Pixel pixel))
            return pixel;
        return null;
// square is dictionary which I use to find a pixel at specific position
    }

Again my goal is to get the i and j values added to themselves 9 times.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

It isn’t clear from your reply if you HAVE or HAVE NOT solved your issue.

I updated the answer below.
And I checked that I am not having any errors.
I just want to know what technic I can use for this goal

Still not entirly clear what you want.
something like
totalValue += ChosenPixelValue.r + ChosenPixelValue.g + ChosenPixelValue.b + ChosenPixelValue.a;
if your pixel type is a color then this will be a FLOAT value sum
if pixel is a vector, then use x y z w instead of rgba, still a float sum
if pixel is a custom type that you created, with integer components, you use your own accessors

Also, if your GetPixel method returns a Pixel, why are then then using GetComponent on the result?

I made another post where I made a little clearer…
https://forum.unity.com/threads/how-to-add-for-int-to-its-total-value.1259708/#post-8002334

Please do not multipost, stay on this thread.

1 Like

Ok thanks for letting me know, I will not do that again.

And also can you help me with this problem.

just define a variable before the loop, add current iterated pixel’s value to the variable in the loop then use the variable after the loop.

1 Like

Hey @vovivoh2007 you’ve given us this snippet:

    public void NeighborPixels(Vector2 pos)
    {
        for (int i = -3 / 2; i <= 3 /2; i++)
        {
            for (int j = -2  / 2; j <= 2 /2; j++)
            {
                if (generateScript.GetPixel(new Vector2Int(Mathf.RoundToInt(pos.x) + i, Mathf.RoundToInt(pos.y) + j)) != null)
                {
                    var ChosenPixelValue = generateScript.GetPixel(new Vector2Int(Mathf.RoundToInt(pos.x) + i, Mathf.RoundToInt(pos.y) + j)).GetComponent<Pixel>().Value;
                    TotalValue += ChosenPixelValue;
                    if (TotalValue < 1)
                    {
                        generateScript.GetPixel(new Vector2Int(Mathf.RoundToInt(pos.x) + i, Mathf.RoundToInt(pos.y) + j)).GetComponent<SpriteRenderer>().color = Color.red;
                    }
                }
            }
        }
    }

And asked how you can add i and j to “total value”.

Why would changing line 10 to TotalValue = ChosenPixelValue + i + j; not solve your issue?

We’re trying really hard here to figure out what you want, but nobody knows what you’re actually trying to accomplish here. What is the value of ChosenPixelValue and what do you want TotalValue to equal?

1 Like

Ohh yea, I forgot about that. Thanks for reminding I appreciate it.

Thanks for your solution, it worked. I appreciate all of your effort.