# SetPixel(int, int, Color, int)

> Sets the pixel color at coordinates ( x, y ).

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public void SetPixel(int x, int y, Color color, int mipLevel)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The x coordinate of the pixel to set. The range is `0` through (texture width - 1).**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The y coordinate of the pixel to set. The range is `0` through (texture height - 1).**** (\[Color]\(/engine/6000.0/script-reference/unityengine/color)): The color to set.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The mipmap level to write to. The range is `0` through the texture's [Texture.mipmapCount](/engine/6000.0/script-reference/unityengine/texture/mipmapcount.md). The default value is `0`.

### Remarks

This method sets pixel data for the texture in CPU memory. [Texture.isReadable](/engine/6000.0/script-reference/unityengine/texture/isreadable.md) must be `true`, and you must call [Texture2D.Apply](/engine/6000.0/script-reference/unityengine/texture2d/apply.md) after `SetPixel` to upload the changed pixels to the GPU.

[Texture2D.Apply](/engine/6000.0/script-reference/unityengine/texture2d/apply.md) is an expensive operation because it copies all the pixels in the texture even if you've only changed some of the pixels, so change as many pixels as possible before you call it.

`SetPixel` might be slower than some other texture methods because it converts the [Color](/engine/6000.0/script-reference/unityengine/color.md) struct into the format the texture uses. To set pixel data more quickly, use [Texture2D.SetPixelData](/engine/6000.0/script-reference/unityengine/texture2d/setpixeldata.md) instead.

The lower left corner is (0, 0). If the pixel coordinate is outside the texture's dimensions, Unity clamps or repeats it, depending on the texture's [TextureWrapMode](/engine/6000.0/script-reference/unityengine/texturewrapmode.md).

If you need to get a large block of pixels, it might be faster to use [Texture2D.SetPixels32](/engine/6000.0/script-reference/unityengine/texture2d/setpixels32.md).

You can use `SetPixel` with the following [texture formats](/engine/6000.0/script-reference/unityengine/textureformat.md):

* Alpha8
* ARGB32
* ARGB4444
* BGRA32
* R16
* R16\_SIGNED
* R8
* R8\_SIGNED
* RFloat
* RG16
* RG16\_SIGNED
* RG32
* RG32\_SIGNED
* RGB24
* RGB24\_SIGNED
* RGB48
* RGB48\_SIGNED
* RGB565
* RGB9e5Float
* RGBA32
* RGBA32\_SIGNED
* RGBA4444
* RGBA64
* RGBA64\_SIGNED
* RGBAFloat
* RGBAHalf
* RGFloat
* RGHalf
* RHalf

For all other formats, Unity ignores `SetPixel`.

Additional Resources: [Texture2D.SetPixels](/engine/6000.0/script-reference/unityengine/texture2d/setpixels.md), [Texture2D.SetPixelData](/engine/6000.0/script-reference/unityengine/texture2d/setpixeldata.md), [Texture2D.GetPixel](/engine/6000.0/script-reference/unityengine/texture2d/getpixel.md), [Texture2D.Apply](/engine/6000.0/script-reference/unityengine/texture2d/apply.md).

### Examples

```csharp
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        Texture2D texture = new Texture2D(128, 128);
        GetComponent<Renderer>().material.mainTexture = texture;

        for (int y = 0; y < texture.height; y++)
        {
            for (int x = 0; x < texture.width; x++)
            {
                Color color = ((x & y) != 0 ? Color.white : Color.gray);
                texture.SetPixel(x, y, color);
            }
        }
        texture.Apply();
    }
}
```
