Trying to get information from multiple gameObjects in a scene

Hope everybody who’s reading this is having a great day!

Anyways I’m having a little bit of a problem with a system I want to implement in my Unity Project, basically it’s a Enemy ID system. The idea was whenever the player enters in contact with a enemy, it would get it’s ID and would use it to instantiate them in a battle scene.

This is the Scriptable Object that I’m using for enemy stats and ID

This is a example of how would the enemy stats look like

And this is the script that check the collisions with the enemies

My problem stand from that I can only get information of one kind of enemy, I’ve tried making the enemy check the collisions so they would get their own ID, it worked but it would be hell to parse this information through scenes. Is there a way to make the script which check collision detect more than one type of enemy? Help would be very much appreciated!

A powerful tool in C# is polymorphism, which allows you to create a generic “type”, which other classes will inherit from. You can then treat them all the same, supposing that the parent type contains the features. For example:

public class Parent
{
    public void DoParentStuff()
    {
        // do stuff
    }
}

// this means it inherits from parent
public class ChildA : Parent
{
    public void DoChildAStuff(){}
}

public class ChildB : Parent
{
    public void DoChildBStuff(){}
}

void Start()
{
    Parent example = new ChildA();
    Parent example2 = new ChildB();

    // valid
    example.DoParentStuff();
    example2.DoParentStuff();
}

If you make all your enemies inherit from a generic “enemy” class, you can call shared methods for all of them without actually specifically knowing what kind of enemy it is.

I’m not to familiarized with tool of C#, but thanks anyways for sharing it, I’ll look it up and try some stuff with so I can get a better idea of how it works!

Just one question and I know that might sound dumb, but I have to do everything of the Parent and Child in diferent scripts and then inherent them from there right?

I’m not sure if I understand what you mean by that, I can give you a more in depth example of how it can be used though.

Each child can call its parent’s methods as if it were its own, and can even have a method “override” the functionality of its parent’s method, so when it gets called, it does something different.

Parent class, with the method that the children can use

public class Enemy
{
    private enemyData data;
   
    public enemyData GetEnemyData()
    {
        return enemyData;
    }
}

Child class, which you can call GetEnemyData() with, and will return the enemyData of the child

public class JumpingEnemy : Enemy // <= this to show it's a child of Enemy
{
    public void Jump()
    {
        // do a jump
    }
}

So the JumpingEnemy can do both Jump() and GetEnemyData(), like this

void Start()
{
    JumpingEnemy enemy = new JumpingEnemy();
    enemy.Jump(); // <-- method from itself
    enemyData data = enemy.GetEnemyData(); // <-- method from parent
}

If we wanted to override what it actually does, we can write something like this

public class NullEnemy : Enemy
{
    public override enemyData GetEnemyData()
    {
        return null;
    }
}

So now if we created the NullEnemy and called GetEnemyData(), it would return nuil, no matter if it were an Enemy or a NullEnemy.

void Start()
{
    Enemy e1 = new NullEnemy();
    NullEnemy e2 = new NullEnemy();

    enemyData data1 = e1.GetEnemyData(); // <-- would be null even though it's type Enemy
    enemyData data2 = e2.GetEnemyData(); // <-- would also be null
}

Feel free to ask any more questions that you don’t quite understand about it, although it might take me time to reply.

Oh, on looking at your question again, I think I can answer it.

You don’t need to put anything in another script, you can put everything in a single script if you wanted. It is much cleaner however, to put everything in its own script.

Reading this like that the way this works now makes a lot more sense to me, thank you so much, I already applied it to my project and it worked very well, now I can finally start focusing on coding the actual main Mechanic of the game instead of fixating myself in other stuff!

Hope you’re having a great day!