Errors When Instancing a Bullet Object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BiLLz

I am super new to Godot and am still trying to learn it using C#. I have a character that runs around and jump. I am trying to make it that whenever the player hits spacebar, their gun shoots. I currently have the bullet scene added as a node of the weapon scene, and the code I use for firing the weapon is:

public void FireWeapon()
{
    
    firingDelay--;

    if ((Input.IsActionJustPressed("moveShoot")) && (firingDelay < 0))
    {
        var newBullet = (StaticBody2D)Bullet.Instance();
        AddChild(newBullet);
    }

}

However, when I press spacebar, I get the following error:

E 0:00:27.237   void Weapon.FireWeapon(): System.NullReferenceException: Object reference not set to an instance of an object.

<C++ Error> Unhandled exception
<C++ Source> /Users/willbur/Desktop/Personal/Computer Science/Godot Engine/PROJECTS/Comfy Couch/SCENES/WEAPONS/Weapon.cs:56 @ void Weapon.FireWeapon()()
Weapon.cs:56 @ void Weapon.FireWeapon()()
Weapon.cs:37 @ void Weapon._Process(Single )()

As I am new to Godot, I have no idea if I am approaching this correctly, so any and all help is appreciated!

:bust_in_silhouette: Reply From: coBearCoding

Hey! answering to your question in Godot C#, you need to instance your scene before you instanciate a object from it. It is suggested to give its own logic to the bullets for actual movement of them. Basicly in the example im calling the script generated and called Bullet.cs

Using your Firing Example it would be like this:

Declaration outside Ready() or Physcis_Process

 private PackedScene _Bullet = (PackedScene)GD.Load("res://Bullet.tscn");

In actual FireWeapon:

private void FireWeapon()
{
    firingDelay--;

     if ((Input.IsActionJustPressed("moveShoot")) && (firingDelay < 0))
    {
       var newBullet = (Bullet)_Bullet.Instance();
       GetParent().AddChild(newBullet);
    }
}

So im using the GetParent() function to get the object that’s executing the action of firing, then with it I can set its position to it, adding it as a child. You can refer this explanation for a more detailed comparison on the official Godot Docs

You can look at my work with the Godot Engine at my Youtube Channel: bearcoding
Hope this helps you! and continue your good work with the Godot Engine ! :D!

As an addition since your new to the Godot Engine, I’ll suggest learning GDScript for you, C# is great… but in Godot is still being implemented, so it’s not as compatible as it is with GDScript and often you will have to look for a workaround of a part that GDScript does with a very simple context and syntax, if performance is an issue for you, feel good to know that C# and GDScript perform really well, and use cases of them are really a niche decision that will supply both programming languages preferences and project needs :3

coBearCoding | 2020-04-29 04:42

Ok, I did that and I no longer get an error. However, I added the weapon as a child node of my player, and now my player glitches up and down, his movement is occasionally sporadic and fast, and he gets stuck on the platforms. Any idea why that is happening?

BiLLz | 2020-04-29 17:00

Hi, since I have no idea of how you’ve coded the whole logic, I’m not sure of the error you’re getting ^^, but well you should try to split the logic for each actor, you can put the logic for turning (moving o jumping) into the player and leave the weapon without any of it, since it is a child of the player it will inherit its position, for the bullet you would want to put it as a child of your weapon instead, so it goes out of the muzzle (if you have a gun) inside the gun you can create an empty 2d Node for being specific a position2D right in the top of the muzzle so you can spawn the bullet there it will inherit both position of the muzzle and at the same time the weapon will get the position of the player ^^

coBearCoding | 2020-05-12 01:24