ColorRect added via script is invisible and corrupts existing Sprite2D texture in Godot 4.6

Godot Version

4.6.3

Question

I’m trying to dynamically create a ColorRect as an aiming reticle. The ColorRect never appears on screen, and adding it causes my existing Sprite2D (a placeholder square for the player character) to change from solid gray to a black-and-white gradient.

aim_box = ColorRect.new()
aim_box.size = Vector2(80, 80)
aim_box.color = Color.RED
aim_box.z_index = 10
get_tree().current_scene.add_child(aim_box)
aim_box.position = global_position - aim_box.size / 2

In Godot 4.6.3 (2D project), when I create a ColorRect via script and add it to the current scene, the ColorRect never appears even though it exists at the correct position with a solid color. At the same time, an existing Sprite2D that was displaying a solid gray GradientTexture2D suddenly changes to a black-and-white gradient. I’ve tried setting z_index, changing colors, and adding to different parents, but the issue persists. What could cause a dynamically added ColorRect to be invisible and corrupt the texture of an unrelated Sprite2D?

What is your current scene’s node type? Are you applying any materials that may affect child nodes or the screen-space?

Thanks for the follow-up. The root scene is a Node2D. There were no custom materials applied to any nodes.

I ended up solving the issue by avoiding ColorRect entirely. I switched to a separate Node2D scene with a _draw() function that draws the rectangle using draw_rect(). The Node2D is instantiated and added to the current scene via script, and it now renders correctly in world space without interfering with any other Sprite2D textures.It seems the problem was related to mixing Control nodes (like ColorRect) into a 2D world-space scene, causing the ColorRect to be off-screen and somehow corrupting the GradientTexture2D on an unrelated Sprite2D. Using a plain Node2D with _draw() sidesteps those issues entirely.