You forgot to initialize the spriteNode

When I tried to get the sprite to reverse when going left, it wouldn’t and I got the error message that something in the line was null. I noted that you never initialized the sprite node in the Ready method. It should be done as follows: spriteNode= GetNode(“Sprite3D”);

I don’t know why your code worked without the initialization. No one else I believe has commented on this. Am I the only one where it didn’t work?

Hi Mark,

Can you post the associated code and any relevant screenshots please as it might be that something is incorrect somewhere in the script as to why the sprite node is not being initialized for you.

Thanks in advance

It is your own code seen in the video about Flipping the Player. My code matches yours as far as I know. And I noticed that you did not instantiate the spriteNode in the Ready method. I went ahead and instantiated it and everything worked fine. I’m just wondering how your code worked as seen in the video. Where did you instantiate the variable?

Hi,

at the top where we have the exports of the required nodes, i believe there was an export to the inspector to drag the srpite node into.

[Export] public Sprite3D SpriteNode { get; private set; }

do you have that line there?

should give you something like the following in the inspector of the player scene.

image

just wondering if that has been populated or not.

Darren

1 Like

This is what the top of the lesson code shows for flipping the sprite:

So, I modified it so it would work, as shown in the second image.

You don’t need to initialize the node in code if you are dragging it in via the inspector as an export variable. Just like the animation player node. In the course, he drags the reference in via the inspector.

1 Like

Hi Mark,

As Darren was indicating and swordboys923 as stated we are using the [Export] prefix which makes the field visible in the inspector so you can just drag and drop the sprite into this field and it can use it.
The reason you get a null without your line of code is indicative that the sprite field in the inspector is empty.
You can by all means use the GetNode but then you wouldnt need the [Export] prefix.

I wonder if you have come from a unity background? If this is the case then [Export] is exactly like when we use [SerializeField] just in case this helps with the understanding.

Hope this helps with the clarity as to why we dont need to get the reference to the sprite node in this case.

I see it now. I must have missed something in the directions. And yes, I have 3 years of experience in Unity programming C#. I didn’t know that [Export] is the same as {SerializeField]. I see where the field in the inspector was not filled in with the Sprite Node. Thanks for clarifying things. So far, I think the way that Godot treats C# is rather weird and I’m going to have to learn a lot more about the differences from Unity. Is there a reference somewhere that shows how I have to modify the way that I use C# in Godot as opposed to Unity?

1 Like

Hi Mark,

There is not anything like a cheat sheet that i know of, I’ve just picked up a few things after using both engines and noticed similarities over the years of being a teaching assistant and using Godot.
Mostly its the differences between the Godot API and Unity API that changes>

If there is anything however that you are not clear on please do reach out as Ryan our new teaching assistant will be happy to help or i will if i am available :slight_smile:

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

Privacy & Terms