Search Unity

Trying to get information from multiple gameObjects in a scene

Discussion in 'Scripting' started by Fiquso, Oct 24, 2021.

  1. Fiquso

    Fiquso

    Joined:
    Oct 13, 2021
    Posts:
    9
    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!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    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:

    Code (CSharp):
    1. public class Parent
    2. {
    3.     public void DoParentStuff()
    4.     {
    5.         // do stuff
    6.     }
    7. }
    8.  
    9. // this means it inherits from parent
    10. public class ChildA : Parent
    11. {
    12.     public void DoChildAStuff(){}
    13. }
    14.  
    15. public class ChildB : Parent
    16. {
    17.     public void DoChildBStuff(){}
    18. }
    19.  
    20. void Start()
    21. {
    22.     Parent example = new ChildA();
    23.     Parent example2 = new ChildB();
    24.  
    25.     // valid
    26.     example.DoParentStuff();
    27.     example2.DoParentStuff();
    28. }
    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.
     
  3. Fiquso

    Fiquso

    Joined:
    Oct 13, 2021
    Posts:
    9

    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!
     
  4. Fiquso

    Fiquso

    Joined:
    Oct 13, 2021
    Posts:
    9
    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?
     
  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    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
    Code (CSharp):
    1. public class Enemy
    2. {
    3.     private enemyData data;
    4.    
    5.     public enemyData GetEnemyData()
    6.     {
    7.         return enemyData;
    8.     }
    9. }
    Child class, which you can call GetEnemyData() with, and will return the enemyData of the child
    Code (CSharp):
    1. public class JumpingEnemy : Enemy // <= this to show it's a child of Enemy
    2. {
    3.     public void Jump()
    4.     {
    5.         // do a jump
    6.     }
    7. }
    So the JumpingEnemy can do both Jump() and GetEnemyData(), like this
    Code (CSharp):
    1. void Start()
    2. {
    3.     JumpingEnemy enemy = new JumpingEnemy();
    4.     enemy.Jump(); // <-- method from itself
    5.     enemyData data = enemy.GetEnemyData(); // <-- method from parent
    6. }
    If we wanted to override what it actually does, we can write something like this
    Code (CSharp):
    1. public class NullEnemy : Enemy
    2. {
    3.     public override enemyData GetEnemyData()
    4.     {
    5.         return null;
    6.     }
    7. }
    So now if we created the NullEnemy and called GetEnemyData(), it would return nuil, no matter if it were an Enemy or a NullEnemy.
    Code (CSharp):
    1. void Start()
    2. {
    3.     Enemy e1 = new NullEnemy();
    4.     NullEnemy e2 = new NullEnemy();
    5.  
    6.     enemyData data1 = e1.GetEnemyData(); // <-- would be null even though it's type Enemy
    7.     enemyData data2 = e2.GetEnemyData(); // <-- would also be null
    8. }
    Feel free to ask any more questions that you don't quite understand about it, although it might take me time to reply.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    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.
     
  7. Fiquso

    Fiquso

    Joined:
    Oct 13, 2021
    Posts:
    9
    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!