Question by
robertbarclay · May 02, 2017 at 08:26 PM ·
errorphysics
Physics.Overlapsphere giving me a null reference exception
Hi guys I need to fix this ASAP and I have no Idea where I'm going wrong would really appreciate the help <3
public class Grenade : MonoBehaviour {
public float fusetime = 1.0f;
public int Damage = 100000;
EnemyHealth enemyHealth;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
float timex = Time.deltaTime;
fusetime -= timex;
if (fusetime <= 0.0f)
{
Collider[] cols = Physics.OverlapSphere(this.transform.position, 1000f);
foreach (Collider col in cols)
{
if (!col) continue;
if (col.tag == "enemy")
{
enemyHealth.Dead();
}
}
Destroy(this.gameObject, 5.5f);
}
}
}
Comment
Answer by robertbarclay · May 02, 2017 at 09:09 PM
Would love some help as I need to fix this by tonight.
Fixed it
public float fusetime = 1.0f;
public int damage = 60;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update()
{
fusetime -= Time.deltaTime;
if (fusetime <= 0.0f)
{
Collider[] cols = Physics.OverlapSphere(gameObject.transform.position, 5f);
foreach (Collider col in cols)
{
Debug.Log(col.gameObject);
if (!col) continue;
if (col.tag == "enemy")
{
// var enemy = GameObject.FindGameObjectsWithTag("enemy");
// foreach(GameObject en in enemy)
// {
col.gameObject.GetComponent<EnemyHealth>().TakeDmg(damage, transform.position);
Destroy(this.gameObject);
// }
}
}
Destroy(this.gameObject, 5.5f);
}
}
Your answer
Follow this Question
Related Questions
Can someone please tell me whats wrong with this?, because when i put it says error in the GetAxis 1 Answer
unity 5.2.1f1 error result->dynamic bounds 0 Answers
Physics object go through the mesh on animation play 1 Answer
Objects wont stop bouncing if they fall. 0 Answers
How to Destroy a PhysicsJoint Object in new Unity.Physics? 0 Answers