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().DashAttack;
Enemy = this.gameObject.GetComponent().Enemy;
EnemyHit = this.gameObject.GetComponent().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().NormalAttack;
ChargedAttack = Player.GetComponent().ChargedAttack;
DashAttack = Player.GetComponent().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().EnemyHealth-= 8;
EnemyInv = 0.1f;
}
if(NormalAttack == true) {
Enemy.gameObject.GetComponent().EnemyHealth-= 5;
EnemyInv = 0.1f;
}
if(ChargedAttack == true){
Enemy.gameObject.GetComponent().EnemyHealth -= 8;
EnemyInv = 0.1f;
}
}
}
}
void OnTriggerExit2D() {
ComboHit = false;
}
}
↧