Unable to change the self_modulate value from the same node in C#.

Godot Version

Godot 4.7

Question

I am very new to Godot and C#. I have run into an issue with changing the self_modulate value (or any other value) of a Sprite2D. Everything I’ve tried doesn’t seem to work. Could some please help with this?

this.self_modulate.a = 1;

The text editor you are using should give you an error message that tells what is wrong. Checking the error message should always be the first step.

In this case the naming convention is not correct. Godot’s C# has slightly different naming style than GDScript and the alpha value in C# is

this.SelfModulate.A

However, you cannot set alpha directly. SelfModulate is a Color and you should set the whole colour when modifying it. For example like this:

    Color newColour = this.SelfModulate;
	newColour.A = 1;
	this.SelfModulate = newColour;

Hope this helps.

Thank you, but this isn’t working either; the error still says:

'Sprite2D' does not contain a definition for 'SelfModulate' and no accessible extension method 'SelfModulate' accepting a first argument of type 'Sprite2D' could be found (are you missing a using directive or an assembly reference?)

This sounds like you are referring to the property wrong, like it’s a method.
Can we see the code you are using to try and access it?

Not quite sure what you mean by “code you are using to try and access it”, so here’s the whole script. The problem is at the end.

using Godot;
using System;
using System.Linq;
using System.Threading.Tasks;




public partial class Sprite2D : Node
{


	public void AddDot(Vector2 position, PackedScene dot)
	{
		Node2D DotInstance = (Node2D)dot.Instantiate();
		DotInstance.Position = position;
		AddChild(DotInstance);
	}

	public async void _on_button_pressed()
	{
		

		Label node = this.GetNode<Label>("MaxLabel");
		int max = node.Text.ToInt();
		node.Visible = false;
		node = this.GetNode<Label>("MinLabel");
		int min = node.Text.ToInt();
		node.Visible = false;		
		node = this.GetNode<Label>("MaxDots");
		node.Visible = false;
		node = this.GetNode<Label>("Minimum dots");
		node.Visible = false;
		HSlider nodey = this.GetNode<HSlider>("MaxSlider");
		nodey.Visible = false;
		nodey = this.GetNode<HSlider>("MinSlider2");
		nodey.Visible = false;
		Button nodeyy = this.GetNode<Button>("Button");
		nodeyy.Visible = false;




	

		PackedScene dot = GD.Load<PackedScene>("res://scenes/game/dot.tscn"); // gets the dot scene


    (int, int)[] dots = {};



		Random rnd = new Random();
		int NumberOfDots  = rnd.Next(min, max);


    // spawns all the dots
    for (int CurNum=0; CurNum<=NumberOfDots; CurNum++)
		{
			int CurX = rnd.Next(1, 19)*100;
			int CurY = rnd.Next(1, 11)*100;
			(int, int)[] Pos = {(CurX, CurY)};
			
			if (!dots.Contains(Pos[0])) 
			{
				dots = dots.Concat(Pos).ToArray();
				AddDot(new Vector2(CurX, CurY), dot);
			}
			else {CurNum-=1;}
			
		}
		await Task.Delay(2000); // gives the player time to see

    Color newColour = this.SelfModulate;
		newColour.A = 1;
		this.SelfModulate = newColour;

	}

}

You are inheriting a Node, which doesn’t have a SelfModulate property.

You need to inherit Sprite2D, I don’t recommend naming your class Sprite2D since that’s already an existing, built-in class used by Godot.

Sorry for this taking so long, but now it’s saying that it’s Unable to cast object of type 'Cover' to type 'Godot.Sprite2D'.

Please show us the code that gives you this error.

Oh, sorry I forgot.

		await Task.Delay(2000); // gives the player time to see
		Node parent = GetParent();
		Sprite2D self = parent.GetNode<Sprite2D>("Cover");
    Color newColour = self.SelfModulate;
		newColour.A = 1;
		self.SelfModulate = newColour;

This code makes no sense. You are trying to get the class you are in. You need to change the class name to be something other than Sprite2D, and then inherit Sprite2D, the built-in class. At the top of your script.

This worked. Thank you so much for your patience!