Hi! Not sure if this question really goes here. I discovered the bug now, but I think it might be related to a previous lesson.
Whenever I kill an enemy which has previously shot a laser, I get this error multiple times (like 30 or 40 per enemy):
UnassignedReferenceException: The variable enemyLaserPrefab of Enemy has not been assigned.
You probably need to assign the enemyLaserPrefab variable of the Enemy script in the inspector.
Though the laser prefab is assigned to the enemies prefabs. It must have something to do with the instanced enemies being destroyed. I don’t get the error if I kill an enemy before it shoots. I’ve compared my Enemy.cs script to the one in this lesson and I can’t see any differences other than variable names (oneEnemyLaser instead of projectile, and so on).
I’m pretty mystified about the nature of these errors. Any insight?
Could you take a screenshot of the error, and maybe include your code as well?
It’s possible that the destruction of the object tried to access one of the spawned lasers, after that laser was destroyed ( at least that’s what I’d guess), but hard to say without seeing the code and where exactly the error is pointing.
Which…have you checked at which line of your code the error is pointing in the console?
Here’s an image of the error, and the code. The error points to the line where the enemyLaserPrefab object is instantiated (line 57 of my code). Weirdly, I changed the order of lines 50 and 51 (the ones inside if (shotCounter <= 0f) { } ), and that reduced the number of errors per enemy killed from around 35 to 1 or 2.
Hi Ruben,
NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.
Don’t assign a laser from the Hierarchy to your enemy because that laser could get destroyed, and the enemy could not shoot anymore.
Further info: if I comment the instantiation of the laser (and the line where I give it velocity), I get errors with the audio (PlayClipAtPoint). Note that this error only appears if I kill an enemy, not before.
UnassignedReferenceException: The variable enemyLaserSFX of Enemy has not been assigned.
You probably need to assign the enemyLaserSFX variable of the Enemy script in the inspector.
UnityEngine.AudioSource.PlayClipAtPoint (UnityEngine.AudioClip clip, UnityEngine.Vector3 position, System.Single volume) (at C:/buildslave/unity/build/artifacts/generated/bindings_old/common/Audio/AudioBindings.gen.cs:866)
Enemy.EnemyShot () (at Assets/Scripts/Enemy.cs:59)
Enemy.CountDownAndShoot () (at Assets/Scripts/Enemy.cs:51)
Enemy.Update () (at Assets/Scripts/Enemy.cs:42)
The other two enemy prefabs have the exact same fields but for the sprite in the sprite renderer, the Explosion Stars Prefab (different colours) and the Offset of the Circle Collider 2D. The referenced EnemyLaser is in the Prefabs folder. The referenced audios are in SFX folder, and the ExplosionParticles(Colour) are in the VFS folder.
The Start method of Enemy.cs has no enemyLaserPrefab, but I checked the correct value in the inspector and that it is correctly instantiated and the instance is used from then on.
Destroy methods in all code files only has as argument instances or the gameObject.
Still getting the errors, but the game runs (both in Windows and web platforms), so I’ll keep on going.
I’m afraid looking at the Inspector is not sufficient here. You must check the value of enemyLaserPrefab during runtime, and you could do that in Start. If your fields in the Inspector contain a reference, that’s good but we do not know what is going on during runtime. Maybe the reference gets lost at some point. We need to figure out when and where that happens.
Destroy methods in all code files only has as argument instances or the gameObject .