NullReferenceException

This newest lesson seems to have caused me some issues. When targeting my new ability, I get an error that reads “NullReferenceException: Object reference not set to an instance of an object
RPG.Combat.Projectile.GetAimLocation () (at Assets/OurScripts/Combat/Projectile.cs:50)
RPG.Combat.Projectile.Start () (at Assets/OurScripts/Combat/Projectile.cs:25)”

I checked the enemy in the ability’s effect radius and he has a collider, which is set to be a trigger. Not sure where else to look. My code should be identical to Sam’s, since I copied and pasted it exactly once I saw I had issues to check if it would fix anything.

When I say “Exactly” I mean my SpawnProjectile code specifically

The problem is on line 50. Check what is on that line and you can determine what could be null

Lines 48 to 56 read:

private Vector3 GetAimLocation()

        {

            CapsuleCollider targetCapsule = projTarget.GetComponent<CapsuleCollider>();

            if(targetCapsule == null)

            {

                return projTarget.transform.position;

            }

            return projTarget.transform.position + Vector3.up * targetCapsule.height / 1.8f;

        }

But the target has a capsule collider, so I’m not sure why it isn’t registering.

If I counted corectly, then it’s not the capsule collider but projTarget that’s null. You need to check if projTarget is null before trying to get components from it

This is line 50

 CapsuleCollider targetCapsule = projTarget.GetComponent<CapsuleCollider>();

I just have some blank lines to make the code easier for me to read.

yes, that’s what I counted. projTarget is null. You need to check that is is not null before you try to get components from it

But shouldn’t the code from this lecture make sure there’s a target with these lines?

Health health = target.GetComponent<Health>();
                if (health)
                {
                    Projectile projectile = Instantiate(projectileToSpawn);
                    projectile.transform.position = spawnPosition;
                    projectile.SetTarget(health, data.GetUser(), damage);
                }

The code from the official project is very similar in Projectile already from what I can tell:

 private Vector3 GetAimLocation()
        {
            CapsuleCollider targetCapsule = target.GetComponent<CapsuleCollider>();
            if (targetCapsule == null)
            {
                return target.transform.position;
            }
            return target.transform.position + Vector3.up * targetCapsule.height / 2;
        }

Actually, the official code I can see in the repo checks if it’s null

        private Vector3 GetAimLocation()
        {
            if (target == null)
            {
                return targetPoint;
            }
            CapsuleCollider targetCapsule = target.GetComponent<CapsuleCollider>();
            if (targetCapsule == null)
            {
                return target.transform.position;
            }
            return target.transform.position + Vector3.up * targetCapsule.height / 2;
        }

Is that the latest code? I believe I looked at the code from this particular lecture, unless I am just looking at especially old code. Either way, I will see if the version you’ve posted works.

It’s the latest code in the repository.

The function is being called in Start. I’m not 100% sure about the order of events here, but Start may be called before your SetTarget is, so that will cause the issue

Yeah, going through the next lecture, that’s when they start adding some of this stuff. So I will get back to this post once I do the next lecture and see if some of these issues resolve.

Figured it out, it wasn’t a code issue at all. I made a very silly mistake and was using a prefab spawn effect instead of a projectile spawn effect.

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

Privacy & Terms