Search Unity

Asign a class name to a variable and get its parameters

Discussion in 'Getting Started' started by Frahnk, May 29, 2023.

  1. Frahnk

    Frahnk

    Joined:
    Apr 4, 2023
    Posts:
    4
    Hello
    Maybe im not being clear, but I want to asign the name of a class to a variable, I tried it this way
    Code (CSharp):
    1. public class Battle_System : MonoBehaviour{
    2.      public Mercenary mercenary;
    3.      public object mer;
    4.  
    5.      void Start(){
    6.           mer = mercenary;
    7.      }
    8. }
    And that worked kinda well, but when I try to access mercenary parameters via mer (like its health), it doesnt work, because it cant access to it.
    Is there a way to achieve this?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,151
    We need to see the code for the Mercenary class.
     
  3. But why? Why don't you access it through the already existing
    mercenary
    "variable"?
     
  4. Frahnk

    Frahnk

    Joined:
    Apr 4, 2023
    Posts:
    4
    Sorry, this is the Merecenary class
    Code (CSharp):
    1. public class Mercenary : Mercenaries
    2. {
    3.      public override void Stats(){
    4.           con = 10;
    5.           pot = 10;
    6.      }
    7.      void Start()
    8.      {
    9.           Stats();
    10.           hPoints = this.con + 100;
    11.           dPoints = this.pot + 5;
    12.       }
    13. }
    And in case is necessary the original Stats:
    Code (CSharp):
    1. public class Mercenaries : MonoBehaviour
    2. {
    3.      public int con;
    4.      public int pot;
    5.      public int hPoints;
    6.      public int dPoints;
    7.      public virtual void Stats(){
    8.           con = 0;
    9.           pot = 0;
    10.      }
    11. }
     
  5. Frahnk

    Frahnk

    Joined:
    Apr 4, 2023
    Posts:
    4
    Because I have three different mercenaries, and I want to access to their constitution (con), depending on the turn (And I dont want to create a really big code that relies on a bunch of if-else)
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    You probably want to put them in an array, then. For that you probably need to understand more about variables and data types, so I'd read up on that a bit.
     
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,569
    Use arrays. Or lists.
     
  8. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    In Unity, you can easily create Prefab (Mercenary) and Variants (Mercenary_1, Mercenary_2, Mercenary_3, you can put them later into array). It's very comfortable. It's good to use the tools you have. Wrap your planning around Unity logic and less regular programming to make life easier.
     
  9. Frahnk

    Frahnk

    Joined:
    Apr 4, 2023
    Posts:
    4
    I have tried arrays before, but then I notice that I just needed to change my perspective, put the mercenaries stats on the arrays, and use them as temporal data. I was trying to access the mercenaries stats directly, big mistake.
    Anyway, thank you for your help c: