The variable has not been assigned (but it has)

I have a script that sets up the world to play in. It has several prefabs assigned to it, and they work flawlessly.
However, when I added a new prefab to the script and assigned it in the inspector, I get an error:

UnassignedReferenceException: The variable copper of 'GameManager' has not been assigned.
You probably need to assign the copper variable of the GameManager script in the inspector.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:73)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineObject.cs:83)
GameManager.SpawnBlocks () (at Assets/Scripts/GameManager.cs:37)
GameManager.Start () (at Assets/Scripts/GameManager.cs:22)

It says that it is not assigned, but it is.

Code below:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
	public GameObject player;
	public GameObject ground;
	public GameObject inventory;
	public GameObject copper;
	private GameCamera cam;
	public int iB = 1;
	public int DBx;
	public int DBy;
	public string serial = "-DEVELOPERS---ONLY-";
	public bool canPlay = false;

	// Use this for initialization
	void Start () {
		cam = GetComponent<GameCamera>();
		SpawnPlayer ();
		SpawnBlocks ();
	private void SpawnPlayer() {
		cam.SetTarget ((Instantiate (player, new Vector3( 0, 3, 0), Quaternion.identity) as GameObject).transform);

	private void SpawnBlocks() {
		DBx = -30;
		DBy = -128;
		while(iB <= 7680) {
			Vector3 position = new Vector3(DBx, DBy, 0);
			Instantiate (ground, position, Quaternion.identity);
			int randomNumber = Random.Range(1, 1000);
			if(randomNumber > 800 && randomNumber < 900) {
				Instantiate (copper, position, Quaternion.identity);
			if(DBy == 1) {
				DBy = -128;

This had me stumped for a while…
what was happening to me was that I had TWO components of the same type assigned to the gameobject. One had the variables assigned, the other did not. I didn’t know the 2nd component copy existed, so I didn’t look go looking for it.

Everything is correct. There is no problems with the code, You must have another gameObject with that script on it. I don’t see any other way it could be :slight_smile:

I had exactly the same issue. Turned out I had this script assigned to my game control object. However I altered the script and added additional properties. I assigned prefabs to my properties, and everything looked fine. However, when I checked my Game Control Object, to which the script was attached, I noticed that all the new properties were unassigned. Clicking the ‘gear’ settings dropdown next to the script assigned to the Game Control Object and clicking ‘reset’ solved the issue.

You are changing your code, but make sure you are APPLY’ing the changes to your prefab after you make them. Otherwise, the prefab that gets instantiated won’t have that inspector assignment completed.

For me it helped to restart the Unity. Probably the correct solution is just reset as written above, but for me restart helped :slight_smile:

This is 2020 Q4. If anyone still facing the problem. Then you should right-click on the script itself in the project view & select the option to view reference in the scene.

This will highlight the objects & separate them out on which your script has been assigned. This particular error occurs only when you have one script applied to two objects unintentionally.

This error is a blessing & can save you from going real mad

I had the same problem, the script was attached to more than one object.

add this line to Awake() function to find all the objects attached to the script:

Debug.Log($“object name: {}”);

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.

Had the same problem. Turns out i accidentally asigned that script to another object.

Make this log line where the error happens

Debug.LogError("SomeVariable has not been assigned.", this);

Then in console you can click the log line and the object will be highlighted in your hierarchy.

For me it was because it was running scripts on deactivated units that were greyed out in the hierarchy. I deleted them and the problem stopped. Then it still threw errors because the prefabs didn’t have them assigned that were not even placed in the scene yet.

In my case, my script was calling the NavMeshAgent’s Stopping Distance to make an editor Gizmo, but the NavMeshAgent Component was being found within the script at runtime and not yet defined in the editor. I fixed this by setting the NavMeshAgent variable to [SerializedField] and then manually dragging the NavMeshAgent component into the field.

Sorry for being late
If anyone still has the same issue, you can try to drag the prefab into the object script, not the script itself
If you have already done this but it does not help, check the name carefully, try restarting unity or even your computer