Create Gnome defender - projectiles not fire

Good day,
Appreciated if someone could shed some light on why the projectiles not able to shoot up when attacker in the same lane, i’ve attached the axe (prefab animation is working) and gun as projectiles under shooter script and event setup and chose fire() .

Gnome animation is working as it, just it doesn’t fire up.

I’ve logged in for debugging with below statement as first line of Fire function

Debug.Log(“projectiles :” + projectiles.name);

and it showed below error

NullReferenceException: Object reference not set to an instance of an object
Shooter.Fire () (at Assets/Scripts/Shooter.cs:66)

Thanks
Alvin

Hi Alvincks,

There is a variable in your IsAttackerInLane method: myLaneSpawner.

Check where a lane gets assigned to this variable. The y-coordinates of both the defender and the spawner must be equal. Otherwise, the condition gets evaluated to false, and nothing gets assigned to myLaneSpawner. If the variable is empty, it is null, thus the NullReferenceException.

Did this help you fix the issue?


See also:

I unticked all spawner and ticked just one(Spawner3) for testing, which has origin transform with position X=10.28, Y=3, Z=9.78

Gnome has transform X=3, Y=3, Z=0

when i start the game, the spawner3 has transform X=-1.969 , Y=0, Z=0 and it’s body showed X=0, Y=0.22, Z=0

Here are what i’ve tried :-

  1. by setting the same Z position to Gnome with Z=9.78, so it it should align to Spawner3(Lizard)
  2. make Z=0 to all Gnome and Spawner

and i got the same error

so i think the spawner and Gnome have the same y-coordinates in this case, i thought could be the Z-position, but still no luck even make both z-position with same value.

You are right, when i saw the cloned spawner (Lizard) came out, the projectiles Axe will disappear, and i can see the cloned lizard at position (-1.253253, 0, 0), in this case, lizard’s y-axis is 0, which is not align to Gnome y-axis since it has value of 3.

my prefab lizard has coordinates in (0,0,0), and animation doesn’t have transform property.

However from the screen, both are them are on y-axis 3, it really crack my head to resolve this

Is something assigned to myLaneSpawner or is that variable null during runtime? Make sure the root game objects are set to (0, 0, 0) in the Inspector in Unity. Otherwise, the position of the children might be confusing as they are relative to their parent. Or did you check the positions via code?

Nina,
I’ve tried re-assigned GameObject projectile to projectiles2 in Start() , and in fire(), i replaced projectile with projectiles2 and it work flawlessly …but i still prefer to find out the root cause, i think we shouldn’t do this hacked approach however i’m not sure how to debug it further as i stuck for 2 days without progress…

I can see only the projectile(Axe) prefab disappear during runtime, and can’t find any suspicion in y-coordinates which caused the concern.

PS : The root game object has set to (0,0,0)

Debugging is not difficult. All you have to do is to verify assumptions. Never assume that something is working just because it looks as though it was working. Log positions into your console. Log the value of conditions into your console. And so on. At some point, you’ll get an unexpected result, and that’s what you could analyse further.

What do you mean by disappearing? Does it disappear from the Hierarchy? Maybe it is just behind the background. Is the z-position of the background set to 10, the z-position of the camera to -10 and the z-position of the other game objects to 0?

I click on the Gnome from hierarchy, during runtime, saw in inspector > shooting script >projectile > Axe (disappear / become empty)

with debug log , here is the result, can see the y-axis are both identical :

myLaneSpawner position :(10.3, 4.0, 9.8)

GameOBject :Gnome, position :(3.0, 4.0, 0.0)
UnityEngine.Debug:Log(Object)
Shooter:Fire() (at Assets/Scripts/Shooter.cs:75)

Blockquote

line 76 show below error : Debug.Log(“projectiles position:”+ projectiles.transform.position);

NullReferenceException: Object reference not set to an instance of an object
Shooter.Fire () (at Assets/Scripts/Shooter.cs:76)

I’m confused. What exactly disappeared? The Axe game object? Could you please share a few screenshots of what you see and mean in Unity?

Does the error refer to this line?

Debug.Log(“projectiles position:”+ projectiles.transform.position);

If so, projectiles is null. Where/how did you assign the object to the variable?


This image captured from edit mode , the Axe prefab has attached to Shooter.cs script as projectiles GameObject

This image captured during runtime, noticed that the attached projectiles has disappeared and y-axis appeared in console are both identical , null exception appeared after got true condition from isAttackerInLane() .





 [SerializeField] GameObject projectiles, gun;

    AttackerSpawner myLaneSpawner;
    Animator animator;
    GameObject projectiles2;

// it works only when ressign the projectiles to projectiles2 
private void Start()
    {
        projectiles2 = projectiles;
        SetLaneSpawner();
        animator = GetComponent<Animator>();

    }


 public void Fire()
    {
               Instantiate(projectiles2, gun.transform.position, transform.rotation);
    }

If the y-positions are true, the defender probably found a lane spawner object. You could test that with a Debug.Log to be on the safe side. I assume, though, that the NullReferenceException stems from the projectiles variable, which suddenly becomes null during runtime.

Stop the game. Then remove the object from the projectiles field and reassign it. Make sure that the Axe is not in the scene. Furthermore, make sure that nothing overrides the value of projectiles.

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

Privacy & Terms