- Home /
Enemy variable remains null
I'm writing a knockback script for the enemies, so that they gets knocked back, but for some reason my Enemy gameobject remains null.
The knockback script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
class KnockBackPlayer : MonoBehaviour {
//GameObjects
public GameObject Player;
public GameObject Enemy; //This is the variable that I want to be filled
//Vector Variables
private Vector2 PlayerVec;
private Vector2 EnemyVec;
public Vector2 KnockBackVec;
//Enemy Variables
public bool EnemyHit;
public Rigidbody2D EnemyRB;
//Force Variables
private float Speed;
public float ForceSpeed;
//Dash Attack Variable
public bool DashAttack;
void Update() {
//Vector Declarations
PlayerVec = Player.transform.position;
EnemyVec = Enemy.transform.position;
//Speed Calculation
Speed = ForceSpeed * Time.deltaTime;
//Other Scripts Declarations
DashAttack = Player.GetComponent<AttackControls>().DashAttack;
Enemy = this.gameObject.GetComponent<DamagePlayer>().Enemy;
EnemyHit = this.gameObject.GetComponent<DamagePlayer>().ComboHit;
//Force Calculating by Difference
KnockBackVec = EnemyVec - PlayerVec;
//Knock Back Added to Enemy
if(EnemyHit == true && DashAttack == false) {
EnemyRB.AddForce(KnockBackVec * Speed, ForceMode2D.Impulse);
}
}
}
The script that I'm trying to take the variable from:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamagePlayer : MonoBehaviour {
//GameObjects
public GameObject Player;
public GameObject Enemy; //The variable that I'm trying to copy
//Booleans
public bool NormalAttack;
public bool ChargedAttack;
public bool DashAttack;
public bool ComboHit;
//Floats
private float EnemyInv;
void Update() {
//Hit Boolean Declaratioms
NormalAttack = Player.GetComponent<AttackControls>().NormalAttack;
ChargedAttack = Player.GetComponent<AttackControls>().ChargedAttack;
DashAttack = Player.GetComponent<AttackControls>().DashAttack;
//Enemy Hit Cooldown
if(EnemyInv > 0) {
EnemyInv -= Time.deltaTime;
}
if(EnemyInv < 0) {
EnemyInv = 0;
}
}
void OnTriggerEnter2D(Collider2D EnemyChar) {
if(EnemyChar.gameObject != Player) {
ComboHit = true;
Enemy = EnemyChar.gameObject;
//Damage Dealings
if(EnemyInv == 0) {
if(DashAttack == true) {
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth-= 8;
EnemyInv = 0.1f;
}
if(NormalAttack == true) {
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth-= 5;
EnemyInv = 0.1f;
}
if(ChargedAttack == true){
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth -= 8;
EnemyInv = 0.1f;
}
}
}
}
void OnTriggerExit2D() {
ComboHit = false;
}
}
Answer by Glurth · Mar 22, 2018 at 10:08 PM
It looks like you are making a few assumptions, but are not checking via code, that they are in fact valid.
Enemy = this.gameObject.GetComponent<DamagePlayer>().Enemy;
This line assumes there a a DamagePlayerComponent on the same game object.
You can use the RequiresComponent attribute to ensure this will be the case.
Also,
Enemy = EnemyChar.gameObject;
appears to be the only line where the Enemy value is actually assigned. This line is called only OnTriggerEnter, which is your second assumption, that a triggerEnter event has actually occurred. What behavior do you want for the Enemy variable when you have NOT had a triggerEnter event? Whatever behavior you choose, you'll need to add some code to check for, and handle this condition.
Lastly, I don't see where EnemyChar
is declared, but it looks like you are assuming it is a valid object and that it has a valid gameObject member.
He will never reach this line:
Enemy = this.gameObject.GetComponent<DamagePlayer>().Enemy;
Because the second line in Update is this:
EnemyVec = Enemy.transform.position;
Since Enemy is not set yet Update will ter$$anonymous$$ate with a NullReferenceException on the second line in Update.