Godot Version
Version 4.2.1
Question
I have a script attached to (in this case) a Sprite2D node. How do I get and set the fields of the Sprite2D from the script in C#?
Version 4.2.1
I have a script attached to (in this case) a Sprite2D node. How do I get and set the fields of the Sprite2D from the script in C#?
Hello, set a specific property, simple example:
public class MySpride2D : Sprite2D
{
[Export]
private Texture2D newSpriteTexture;
public override void _Ready()
{
// in this line set new texture to current sprite2d
// access to texture is via Texture property
this.Texture = newSpriteTexture
}
}
So the script must inherit from the node type I want to attach it to?
Yes, exactly.
here is another way to access Sprite2D’s properties without extending the Sprite2D class. the example sets TestSprite’s color to Red.
Sprite2D v_sprite = GetNode<Sprite2D>("TestSprite");
v_sprite.SelfModulate = Colors.Red;
So in that example the script is attached to the Sprite2D node named “TestSprite”? Seems like if there is a script attached to a node then the node is no longer retrievable in the node tree, only the script can be found at that path. So this would give an invalid cast exception