UnassignedReferenceException: The variable has not been assigned, even though it has. - c#

I have a script that controls shooting and it’s assigned to a prefab. I hit play to see if it works and it says this when I shoot:

UnassignedReferenceException: The variable player of BulletController has not been assigned.
You probably need to assign the player variable of the BulletController script in the inspector.
UnityEngine.GameObject.GetComponent[PlayerController] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:35)
BulletController.Start () (at Assets/Scripts/BulletController.cs:22)

However as you can see I’ve assigned my player variable:

I did click apply as well but nothing happened. I tried clicking on the cog icon and clicking reset but it still didn’t work.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class BulletController : MonoBehaviour {


	PlayerController PlayerControllerScript;

	public float speed;
	public float coolDown = 5f;
	public float coolDownTimer;

	public Transform bullet;

	public GameObject player;
	//Rigidbody2D rb;

	
	void Start()
	{
		PlayerControllerScript = player.GetComponent<PlayerController> ();
	}
	void Update()
	{
		Firing ();
		bulletActive ();
	}

	void Firing()
	{
		bullet.position += bullet.up * Time.deltaTime * speed;
	}


	public void OnTriggerEnter2D(Collider2D other)
	{
		if(other.CompareTag("Enemy"))
		{
			Destroy(other.gameObject);
			PlayerControllerScript.score += 10;
			PlayerControllerScript.ScoreText();
		}
		Destroy (gameObject);
	}

Thanks in advance.

Your issue is probably the issue I was having…

Can you see that the object field is bolded? This means it’s differing from the prefab of the same name. You’ve probably assigned an object in the scene to the variable, which it can’t save into a prefab.

Now, there’s two courses of action you can take here:

  1. Turn the player into a prefab and use that prefab on the projectile’s inspector Player variable (But this wastes space in your assets, so I prefer 2)

  2. Use something like this:

    public GameObject Player_Blue;

    void Start() 
    {
        Player_Blue = GameObject.FindWithTag ("Player");
    }
    

Then make sure your player object has the “Player” tag and you should be good to go!

I had this issue after renaming a script, the debugger showed everything as assigned but I got an exception on Instantiate. I had to recreate the prefabs in the end which wasn’t great.

I had the same issue just right click on the player on the inspector and change it to prefab. That will fix the problem.