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.
You should update with the relevant portions of your script, so say lines 1-40(for padding)
I added script to show how I'm using it. I I used the inspector to assign the variable the line of code I used to do it was:
public GameObject player;
Do you have this script on another object maybe? where the player isn't assigned?
Add a Debug.Log call to Start, see if you get more than one log message. That has tripped me up an embarrassing number of times.
Answer by BlackSnow_2 · Sep 02, 2015 at 11:36 PM
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!
Answer by Camikazee · Mar 09, 2017 at 09:00 PM
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.
Answer by unity_hqhvp8Zt6unmhA · Aug 19, 2019 at 04:43 PM
I had the same issue just right click on the player on the inspector and change it to prefab. That will fix the problem.
Your answer
Follow this Question
Related Questions
[HELP C#] Making Gameobject 1 equal Prefab 2 1 Answer
UnassignedReferenceException (Didn't find a solution) 1 Answer
How do i move a cube by one of its Vertices/Vertex 1 Answer
How to assign GameObject to a instantiated prefab via Script(C#)? 2 Answers
Why does Unity lock the transform of prefabs in my scene in playmode? 0 Answers