UnassignedReferenceException Problem in the TD course

Updated on September 13, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
7 on August 31, 2018

I’ve been following the TD course, it’s great BTW, but keep getting this error popping up repeatedly in the console:

“UnassignedReferenceException: The variable partToRotate of Turret has not been assigned.
You probably need to assign the partToRotate variable of the Turret script in the inspector.
UnityEngine.Transform.get_localRotation”

I have searched the help forum here and on the Unity site and I cannot find a solution.

Now, I know what you’re going to say (as I’ve seen in numerous other threads on this topic), “The error message already tells you what’s wrong, you just have to assign it in the inspector”.  I already have, I was going to post a screenshot but that function seems to have been disabled by the forum admin.  But I assure you I have done that.  Any other suggestions?  Current code for my Turret script is as follows:

using UnityEngine;
public class Turret : MonoBehaviour {
[Header ("Attributes")]
public float turnSpeed = 10f;
public float range = 15f;
public float fireRate = 1f;
[Header ("Unity Setup Fields")]
public string enemyTag = "Enemy";
private float fireCountdown = 0f;
private Transform target;
public Transform partToRotate;
public GameObject bulletPrefab;
public Transform firePoint;
void Start()
{
InvokeRepeating("UpdateTarget", 0f, 0.5f);
}
void UpdateTarget()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
float shortestDistance = Mathf.Infinity;
GameObject nearestEnemy = null;
foreach(GameObject enemy in enemies)
{
float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
if (distanceToEnemy < shortestDistance)
{
shortestDistance = distanceToEnemy;
nearestEnemy = enemy;
}
}
if (nearestEnemy != null && shortestDistance <= range)
{
target = nearestEnemy.transform;
}
else
{
target = null;
}
}
void Update()
{
if (target == null)
return;
Vector3 dir = target.position - transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = Quaternion.Lerp(partToRotate.localRotation, lookRotation, turnSpeed*Time.deltaTime).eulerAngles;
partToRotate.rotation = Quaternion.Euler (0f, rotation.y, 0f);
if (fireCountdown <= 0) 
{
Shoot();
fireCountdown = 1f / fireRate;
}
fireCountdown -= Time.deltaTime;
}
void Shoot()
{
GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Bullet bullet = bulletGO.GetComponent<Bullet>();
if (bullet != null)
bullet.Chase(target);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, range);
}
}
  • Liked by
Reply
6 on September 1, 2018

Are you assigning it in play mode?
And have you assigned it for every turret in the scene?

on September 2, 2018

Yup, and specifically to the prefab(s) it’s associated with and hit apply. This started with the first turret I built and hasn’t gone away (as I’d hoped). If it helps, the game will still work just fine, but is throwing the error code about 1000 of them in 1 min of run time.

Wise
on September 2, 2018

So wait. You are assigning it in play mode, and the turret still rotates anyway, even with the error?

on September 4, 2018

I’ve tried assigning it in both play and edit mode to see if it made any difference. It didn’t. Yes the turret functions properly and rotates on the correct axis. The only issue is it throws this annoying error code about 2 times per second. (Ps. Thanks for the help and being patient with me. As a hobbiest and pretty new to this I’m sure my descriptions/answers can be frustrating).

Wise
on September 4, 2018

Ok, so you should only be assigning it in edit mode, changes to objects in play mode don’t get saved.

And if it’s still rotating, that means it IS being set. So there is some rogue Turret script in the scene somewhere. You should probably find it to properly solve your issue, but to stop the error messages, add this to the end of your Start function

 

if (partToRotate == null){
print(gameObject.name);
Destroy(this);
}

 

This will give you the name of the object with the unset reference, and delete the script to stop the errors. But the script will be there again unless you remove it in edit mode.

on September 5, 2018

Thanks! I was beginning to suspect that there might have been some duplication or something (I seem to remember changing the original name of my turret script and Visual Basic not liking it). But with that code snippet I should be able to track it down now! Awesome and thanks again, will report back.

on September 13, 2018

You, sir/madame, are a lifesaver!

This totally worked.  And an awesome snippet of code for debugging, I am sure I will be using it for years to come.

Turns out the problem wasn’t in any of the scripts, but a stupid clerical error on my part.

When making my game field I made about 255 game node prefabs, but didn’t delete my original which still had the turret script on it that shouldn’t be called until my turret is instantiated on it.  Your debug code pointed me right to it (Node 35), deleted it from the hierarchy, and replaced it with a fresh prefab, and bingo bango boingo, everything works perfectly.

 

thanks again, that error was driving me crazy

 

Show more replies
  • Liked by
Reply
Cancel