- Home /
UnassignedReferenceException
I have a bomb explosion script where i press space to make a bomb explode.
@Llama_w_2Ls yeah sure
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BombScript : MonoBehaviour
{
public float fieldofImpact;
public float force;
public LayerMask LayerToHit;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Test");
explode();
}
}
void explode()
{
Collider2D[] objects = Physics2D.OverlapCircleAll(transform.position, fieldofImpact, LayerToHit);
foreach(Collider2D obj in objects)
{
Vector2 direction = obj.transform.position - transform.position;
obj.GetComponent<Rigidbody2D>().AddForce(direction * force);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, fieldofImpact);
}
}
When i press space i get the "Test" message in console along with: UnassignedReferenceException: The variable explosion of ExplodeBomb has not been assigned. You probably need to assign the explosion variable of the ExplodeBomb script in the inspector.
The script is assigned only to the bomb object. If i execute the explode function in void update with no condition before, it works, in the way that it continuously explodes, of course.
Tried using both Unity 2019 & 2020.
Could you post the entire bomb script, including all the public variables
You haven't provided the correct script.... The one throwing the error is ExplodeBomb
not BombScript
@Hellium you are right, i got the scripts messed up. I removed "Explode Bomb" and left only "BombScript" and i no longer get the UnassignedReferenceException but i get the same behaviour: explode(); works in void update, does not work on button or mouse press.
Your answer
Follow this Question
Related Questions
Unassigned Reference Exception ? 0 Answers
Inspector Error 1 Answer
Why has my BinaryFormatter stopped working? 2 Answers
"Load Data from Excel with Odbc" doesn't work in Unity 5 64bit 3 Answers