- Home /
Help with error: UnassignedReferenceException
Edit, I assigned references to both Flames, and the message keeps poping up.
This is the error:
UnassignedReferenceException: The variable leftFlame of 'TurretControl' has not been assigned.
You probably need to assign the leftFlame variable of the TurretControl script in the inspector.
TurretControl.Awake () (at Assets/Scripts/TurretControl.js:13)
This is the script:
//Turret control script
var target : Transform; var range = 10.0; var leftFlame : GameObject; var rightFlame : GameObject;
static var mode = "idle";
function Awake() { target = GameObject.FindWithTag("Player").transform; leftFlame.renderer.enabled = false; rightFlame.renderer.enabled = false; }
function Update () { if(target && CanAttackTarget()) { //transform.LookAt(target); var targetRotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 1.2); } }
function CanAttackTarget() { //check if the target is close enough if(Vector3.Distance(transform.position, target.position) > range) { Disengage(); return false; }
var hit : RaycastHit;
//check if there is collision inbetween turrent and target
if(Physics.Linecast(transform.position, target.position, hit))
{
if(hit.collider.gameObject.tag != "Player")
{
Disengage();
return false;
}
else
{
Attack();
return true;
}
}
return true;
}
function Attack() { if(mode != "attack") { InvokeRepeating("FalconAnimate", 2, 0.05); mode = "attack"; } }
function Disengage() { if(mode != "idle") { CancelInvoke(); mode = "idle"; leftFlame.renderer.enabled = false; rightFlame.renderer.enabled = false; } }
function FalconAnimate() { if(leftFlame && rightFlame) { if(leftFlame.renderer.enabled) { leftFlame.renderer.enabled = false; rightFlame.renderer.enabled = true; } else { leftFlame.renderer.enabled = true; rightFlame.renderer.enabled = false; } } else { print("Effects on turrent not set!"); } }
Answer by corpsinheretoo · Mar 31, 2014 at 05:05 AM
I was having the same problem and this thread helped me out. But I would like to restate clearly my mistake so that it is clear for others:
I was dragging my prefab (from the project view) onto the inspector while having selected the script in the project view (which is wrong);
this looks like it works (the prefab name shows up in the variable slot) - but does nothing. I guess this was sort of like combining blueprints; instead, I dragged my prefab (again from the project view) onto the inspector while the instance of my script (in the scene hierarchy) was selected. Then all was good again :).
Thanks man! I was banging my head around for days and when I applied the solution everything was good. :-)
O$$anonymous$$G thank you!! You saved me even more hours of confusion!!!
Answer by DaveA · Apr 22, 2011 at 12:40 AM
Did you do that, assign the leftFlame variable of the TurretControl script in the inspector?
You'd be surpised how many times he has to be told that. time.http://answers.unity3d.com/questions/54110/how-to-disable-errors-in-the-console/54112#54112
Im doing this with a Tutorial, and the guy never assigned anything in the inspector from what I can tell.
Answer by Gabriel 5 · Apr 22, 2011 at 01:03 AM
Im doing this with a Tutorial, and the guy never assigned anything in the inspector from what I can tell.
He must have meant to because the code seems to assume that you do because there are 0 checks to see if there is a reference and/or find the reference if there is not. I would try assigning the reference in the inspector.
I edited the post, I assigned references, but the problem persists.
Answer by eloka · Jul 14, 2011 at 10:00 AM
had that problem too here is what i did. i included a new variable 'x' and assigned the false value in my function update and then changed its value so it doesnt update its value again.
var target : Transform;
var range = 10.0;
var leftFlame : GameObject;
var rightFlame : GameObject;
var mode = "idle";
var x = "t";
function Awake()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update ()
{
if( x =="t")
{
leftFlame.renderer.enabled = false;
rightFlame.renderer.enabled = false;
x="s";
}
Answer by Brian1957 · Aug 03, 2013 at 05:50 PM
Should it be of help, I have just resolved my 'UnassignedReferenceException'. For those using the same book. To cut a long story short, the solution was that, I have a bullet prefab, an explosion prefab and a moveBullet scipt. The moveBullet script was changed to show the explosion prefab. You will easily see the explosion variable displayed at the top of the inspector window and will have dragged and dropped the explosion prefab to the variable. Now you will have to go back to the original bullet prefab find the same movebullet script in the inspector window for that prefab and add the explosion prefab to the script a second time. N.B. I am a total beginner and the book is Holistic Game Development with Unity