#I'm trying to build snake

8 messages · Page 1 of 1 (latest)

empty latch
#

So far what I have been building has been working fine. But I have no idea where this error is coming from and I don't want it to brick anything later down the line.

I'm planning on using Panel segments as parts of the snake, I haven't gotten to movement or much else yet.

Thanks for all the help
Code

public partial class Main : Node
{
    [Export]
    public PackedScene SnakeScene { get; set; }

    private List<Panel> Snake = new List<Panel>();

    public override void _Ready()
    {
        AddSegment(new Vector2(200, 200));
        GenerateSnake();
        foreach (Panel item in Snake)
        {
            GD.Print(this.ToString());
        }
    }
    public void AddSegment(Vector2 pos)
    {
        Panel test = (Panel)SnakeScene.Instantiate();
        test.Position = pos;
        Snake.Add(test);
        AddChild(test);
    }
    public void GenerateSnake()
    {
        for (int i = 0; i < 3; i++)
        {
            AddSegment(new Vector2(450,450+i*50));
        }
    }
}```
Error
E 0:00:00:813   void Main.AddSegment(Godot.Vector2): System.NullReferenceException: Object reference not set to an instance of an object.
  <C# Error>    System.NullReferenceException
  <C# Source>   Main.cs:37 @ void Main.AddSegment(Godot.Vector2)
  <Stack Trace> Main.cs:37 @ void Main.AddSegment(Godot.Vector2)
                Main.cs:19 @ void Main._Ready()
#

I'm not sure what is causing the error since everything seems to be working ok

languid ingot
#

you might have accidentally assigned this script to two different nodes and one doesn't have SnakeScene set

#

but that's just a guess

empty latch
#

Thanks, that was it.

#

I added a timer node that I wasn't using yet, in the main.cs . It didn't have the SnakeScene.tscn

languid ingot
#

it's a common issue; the tell is "export is null even though it was set in editor" and "everything still seems to work" because one script throwing an error doesn't affect other scripts

empty latch
#

huh, interresting. I thought everything would work out because the timer node wasn't interacting with the panel node at all.