[C#] Assigment results in null

Godot Version

v4.6.dev5.mono.official [f5918a9d3]

Question

I don’t understand why assigning the character.CameraTarget to CameraController.Instance.Target does not work. It assigns null.

Is this some Godot C# related “specialty”? :thinking:

Character:

[GlobalClass]
public partial class Character : CharacterBody3D
{
    [Export] public Marker3D CameraTarget { get; private set; }

PlayerController:

public partial class PlayerController : CharacterController
{

    public override void _Ready()
	{
        var character = GetTree().GetFirstNodeInGroup("character") as Character;
		if (character != null)
		{
			TakeControl(character);
			CameraController.Instance.Target = character.CameraTarget;
		}
	}

CameraController:

[GlobalClass]
public partial class CameraController : Node
{
    public static CameraController Instance { get; private set; }


    [Export] public Marker3D Target { get; set; }


    public override void _EnterTree()
    {
        Instance ??= this;
    }

Never heard of the valid assignment just resulting in null. Either the CameraTarget is null, or the assignment just never reached. Take a debugger and step through your code to see what is going on.

That’s embarassing, I actually forgot to assign the camera target in the inspector….
Don’t code when you are really tired. :unamused_face:

Thank you!

Yes, “take another look in the morning” is a commonly used technique.

1 Like