Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Generating Type of Class OnClick() event

Discussion in 'Scripting' started by kitsune9000, Apr 13, 2018.

  1. kitsune9000

    kitsune9000

    Joined:
    Aug 15, 2015
    Posts:
    42
    I need help. I have two classes, Warrior and Healer (for example's sake.)
    Both Classes use a random.range system to set starting attributes and gender. For example, the Warrior class is something like this:
    public int gender;
    public in health;

    //set gender through if, return
    //set health through if, return

    Next, I have a menu system, and the goal is for the player to trade his coin for being able to recruit a random instance of either the Warrior or Healer (player's choice)

    Thus, I have corresponding buttons. The 'Recruit Warrior' Button and 'Recruit Healer' both have the RecieveCharacter.cs attached to their button, with a public function that checks which public variable has been set for adventurerClass. So it is something like this:

    public string adventure class;
    ...
    public void theFunctionTheButtonUses(){
    if(adventureClass =="Warrior"){
    //generateclass
    }
    }

    ...and you get the idea. Now comes my question... I want to instantiate an instance of the class the player picks (either warrior or healer) and I want to ensure the stats are randomized like I put in my code. The classes work fine, and the button works fine. I'm not having problems with broken code... I just don't know the syntax to implement my instance. I have an empty game object called 'AdventurerGameObject' set to 0,0,0 in my scenes. I wanted a class instance to spawn at runtime OnClick, but I don't use prefabs. I wanted to class instance to directly be created as a child of the AdventurerGameObject. I also need to check for gender and set the according portrait and stats to display in a panel on it's own, though I haven't gotten to that point yet.

    I've been looking for help in documentation and all I can find is instantiating objects from prefabs. Any help or direction towards a tutorial would help me. Thank you.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can instantiate a prefab and set its parent, if you weren't sure about that.
    You can create a new game object and assign components to it.
    You can instantiate a game object using another game object that's already in the scene.
    And if you just want to attach a script (or alter its stats) and assign a portrait, then you can just.. do that. That wouldn't even necessarily require instantiating everything, provided that you have a child game object there that you can use.

    So, choose whichever of those that makes sense.

    As for the stats, once the object is there, you can use a method to setup the starting stats.

    Please clarify if I misunderstood something or add any information which may better help to pinpoint your issue (/question). :)
     
  3. kitsune9000

    kitsune9000

    Joined:
    Aug 15, 2015
    Posts:
    42
    Thanks for the reply. I ended up just using a structure, same thing I guess, really... and get/set at my variables, then public Warrior (string name, int stat1, int stat2, ect..), private Warrior and this. with random.range... then public static return new Warrior name, and finally making a method that returns when invoked by another script attached to my button. That's in summary of course, but essentially when I use a constructor that I create myself, it is not null since I fed it values, correct?, and not up for garbage collection since I'm not doing anything in particular with it? So if I have used:
    WarriorClass warrior = WarriorClass.GetRadomChar("Robert");
    ...then I have created Robert, and he essentially exists without fear for anything happening to him?
    I was planning on doing as you say, methos, and feeding his stats into a display within the built-in UI.

    I don't think there is anything other than that I have a question on, so thank you.