Setting color of Polygon2D in gdscript not working

Godot Version

4.2.2.stable.official

Question

I have a class called DamageZone deriving from Area2D that is added to my scene programmatically. I’m adding 2 child nodes to it in the _init function: a CollisionPolygon2D to be able to check for overlapping bodies, and a Polygon2D to be able to show the area in the game.

I can see the Polygon2D appearing in the scene when I play, but even though I’m setting the color to a semi-transparent orange, it is always opaque and white. Can anyone help me figure out what I’m doing wrong?

Here’s the code of my class:

extends Area2D

class_name DamageZone

const DEFAULT_COLOR: Color = Color(186, 90, 29, 142)

var collision_polygon: CollisionPolygon2D
var polygon: Polygon2D

func _init(p_vertices: PackedVector2Array):
	collision_polygon = CollisionPolygon2D.new()
	collision_polygon.set_polygon(p_vertices)
    add_child(collision_polygon)

	polygon = Polygon2D.new()
	polygon.set_polygon(p_vertices)
	polygon.z_index = 2
	polygon.set_color(DEFAULT_COLOR)
	add_child(polygon)

How about using polygon.color = DEFAULT_COLOR or polygon.modulate = DEFAULT_COLOR?

Thanks for the suggestion, these didn’t work either but it led me to find the solution! I searched for modulate and found a post saying that the constructor for Color expects values between 0 and 1, not 0-255 like I thought.

Changing

Color(186, 90, 29, 142)

to

Color(0.73, 0.35, 0.11, 0.55)

gave me the correct color.

Color8() takes 0 to 255 values, if you are looking for such a function

2 Likes

This should be the issue.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.