- Home /
Enemy ID System
Hello and I'm hope whoever is reading this is having a wonderful day today!
So as the title says, I want to create a enemy ID system, nothing to crazy, I have this Scriptable Object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//General data for "generic" enemies in the game.
[CreateAssetMenu(fileName = "enemyData")]
public class enemyData : ScriptableObject
{
public string enemyName;
public int enemyHP;
public int enemyCurrentHP;
public int enemyDamage;
public int enemyID;
}
And another script that basically makes that when the player collides with a certain enemy it loads a battle scene, and the enemy in the battle would be based on the enemyID in the SO.
I'm just having problems getting the enemy ID in the time of collision, what is the best way to do something like that?
what kind of ID are you searching for? something to identify this specific instance of an enemy you collided with or something to identify what kind of enemy you collided with?
Answer by andrew-lukasik · Oct 21, 2021 at 09:49 PM
Guard.cs
using UnityEngine;
public class Guard : MonoBehaviour
{
void OnCollisionEnter ( Collision collision )
{
var isBaddie = collision.gameObject.GetComponentInParent<ICanHaz<BaddieData>>();
if( isBaddie!=null )
{
BaddieData data = isBaddie.CheezBurger;
if( data!=null )
Debug.Log($"{data.Label}, stop right there criminal scum!");
}
}
}
Baddie.cs
using UnityEngine;
public class Baddie : MonoBehaviour, ICanHaz<BaddieData>
{
[SerializeField] BaddieData _identity = null;
BaddieData ICanHaz<BaddieData>.CheezBurger => _identity;
}
BaddieData.cs
using UnityEngine;
[CreateAssetMenu( fileName="baddie data 0" , menuName="Game/Baddie data" )]
public class BaddieData : ScriptableObject
{
public string Label;
public int ID, InitialHP, HP, Damage;
void Reset ()
{
this.ID = System.Guid.NewGuid().GetHashCode();
this.Label = "Hans";
this.InitialHP = 100;
this.HP = this.InitialHP;
}
}
ICanHaz.cs
public interface ICanHaz <T>
{
T CheezBurger { get; }
}
Your answer
Follow this Question
Related Questions
how do i make an enemy appear then immediately dissapear? 1 Answer
Loading new level after killing enemies 3 Answers
Enemy spawn 1 Answer
How to spawn enemies on a network? 0 Answers