So I have a RPG class select menu with a Create button but I have some stats that I want to attach to the character when he is instantiated.
I added this function to the script that I made in the discussion before and set the playerCharacter to the current character showed.
I wrote this code to the create button:
public void CreatePlayer()
{
Application.LoadLevel(0); // Opening the main scene
Instantiate(playerCharacter);
}
Can someone tell me how to add the stats to the GameObject?
Oh boy! I saw your previous post. It’s a mess and everyone else just added extra mess to it.
You need to have a GameObject which is representing your character in the game, then you need to attach a script to it (in the inspector) like this:
using UnityEngine;
public class CharacterInfo : MonoBehaviour{
public string characterClassName;
public string characterClassDescription;
//Stats
public int stamina;
public int endurance;
public int strength;
public int intellect;
}
then you need to make a prefab with this GameObject by dragging it from the Hierarchy to the Project tap. Then whenever you use Instantiate method to create your character, you get a reference to it which then you can use GetComponent() on it to get access to its Stats like stamina and set it to whatever value you want.
So, first you make your character with the default values for its stats.
After that, you set the stats value.
something like this:
void createCharacter()
{
GameObject myCharacter = (GameObject) Instantiate(characterPrefab);
myCharacter.GetComponent<CharacterInfo>().stamina = 20;
}
@ben.rasooli
This is my player’s code where I describe his stats:
public class BasePlayer {
private string nomeGiocatore;
private int livelloGiocatore;
private BaseCharacterClass classeGiocatore;
private int vitaMassima;
private int vitaAttuale;
private int vitaMinima;
private int energiaMassima;
private int energiaAttuale;
private int energiaMinima;
private int forza;
private int intelligenza;
private int destrezza;
public string NomeGiocatore
{
get
{
return nomeGiocatore;
}
set
{
nomeGiocatore = value;
}
}
public int LivelloGiocatore
{
get
{
return livelloGiocatore;
}
set
{
livelloGiocatore = value;
}
}
public BaseCharacterClass ClasseGiocatore
{
get
{
return classeGiocatore;
}
set
{
classeGiocatore = value;
}
}
public int VitaMassima
{
get
{
return vitaMassima;
}
set
{
vitaMassima = value;
}
}
public int VitaAttuale
{
get
{
return vitaAttuale;
}
set
{
vitaAttuale = value;
}
}
public int VitaMinima
{
get
{
return vitaMinima;
}
set
{
vitaMinima = value;
}
}
public int EnergiaMassima
{
get
{
return energiaMassima;
}
set
{
energiaMassima = value;
}
}
public int EnergiaAttuale
{
get
{
return energiaAttuale;
}
set
{
energiaAttuale = value;
}
}
public int EnergiaMinima
{
get
{
return energiaMinima;
}
set
{
energiaMinima = value;
}
}
public int Forza
{
get
{
return forza;
}
set
{
forza = value;
}
}
public int Intelligenza
{
get
{
return intelligenza;
}
set
{
intelligenza = value;
}
}
public int Destrezza
{
get
{
return destrezza;
}
set
{
destrezza = value;
}
}
}
And this is the creation script of the player:
`using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UI_Creazione_P : MonoBehaviour {
private BasePlayer newPlayer;
public bool isWarriorClass;
public bool isMageClass;
public bool isArcherClass;
public Image Guerriero;
public Image Mago;
public Image Assassino;
public Text Nome_Classe;
public Text Descrizione_Classe;
public Text Forza;
public Text Intelligenza;
public Text Destrezza;
public Button Create;
public GameObject warriorCharacter;
public GameObject archerCharacter;
private GameObject playerCharacter;
void Start()
{
// Modello guerriero
warriorCharacter = GameObject.FindGameObjectWithTag("Warrior_Character");
// Modello arciere
archerCharacter = GameObject.FindGameObjectWithTag("Archer_Character");
// Rendiamo i modelli invisibili all'inizio
warriorCharacter.SetActive(false);
archerCharacter.SetActive(false);
// "Creiamo il giocatore"
newPlayer = new BasePlayer();
// Settiamo le calssi all'inizio della creazione tutte false
isWarriorClass = false;
isMageClass = false;
isArcherClass = false;
}
void Update()
{
// Chiamiamo le funzioni della visione delle stats
RendiVisibileStats();
}
// Funzione che rende la classe guerriera quella attuale
public void SetWarriorClassTrue()
{
isWarriorClass = true;
isMageClass = false;
isArcherClass = false;
// La classe del giocatore sarà quella guerriera
newPlayer.ClasseGiocatore = new WarriorClass();
// Rende visibile il modello del guerriero
warriorCharacter.SetActive(true);
archerCharacter.SetActive(false);
// Il modello 3D sarà quello guerriero
playerCharacter = warriorCharacter;
}
// Funzione che rende la classe maga quella attuale
public void SetMageClassTrue()
{
isMageClass = true;
isWarriorClass = false;
isArcherClass = false;
// La classe del giocatore sarà quella maga
newPlayer.ClasseGiocatore = new MageClass();
// Rende visibile il modello del mag
archerCharacter.SetActive(false);
warriorCharacter.SetActive(false);
}
// Funzione che rende la classe assassina quella attuale
public void SeArcherClassTrue()
{
isArcherClass = true;
isMageClass = false;
isWarriorClass = false;
// La classe del giocatore sarà quella assassina
newPlayer.ClasseGiocatore = new ArcherClass();
// Rende visibile l'assassino
archerCharacter.SetActive(true);
warriorCharacter.SetActive(false);
// Il modello 3D sarà quello arciere
playerCharacter = archerCharacter;
}
// Funzione che racchiude tutte le "stats" del giocatore
void PlayerStats()
{
// Classe giocatore
// Vita giocatore
newPlayer.VitaAttuale = newPlayer.ClasseGiocatore.VitaAttuale;
newPlayer.VitaMassima = newPlayer.ClasseGiocatore.VitaMassima;
newPlayer.VitaMinima = newPlayer.ClasseGiocatore.VitaMinima;
// Energia giocatore
newPlayer.EnergiaAttuale = newPlayer.ClasseGiocatore.EnergiaAttuale;
newPlayer.EnergiaMassima = newPlayer.ClasseGiocatore.EnergiaMassima;
newPlayer.EnergiaMinima = newPlayer.ClasseGiocatore.EnergiaMinima;
// Forza
newPlayer.Forza = newPlayer.ClasseGiocatore.Forza;
// Intelligenza
newPlayer.Intelligenza = newPlayer.ClasseGiocatore.Intelligenza;
// Destrezza
newPlayer.Destrezza = newPlayer.ClasseGiocatore.Destrezza;
}
// Funzione che rende visibile le "stats" nel momento della creazione
void RendiVisibileStats()
{
PlayerStats();
// Nome
Nome_Classe.text = "Nome classe: " + newPlayer.ClasseGiocatore.Nome;
// Descrizione
Descrizione_Classe.text = "Descrizione classe: " + newPlayer.ClasseGiocatore.Descrizione;
// Forza
Forza.text = "Forza: " + newPlayer.Forza.ToString();
// Intelligenza
Intelligenza.text = "Intelligenza: " + newPlayer.Intelligenza.ToString();
// Destrezza
Destrezza.text = "Destrezza: " + newPlayer.Destrezza.ToString();
}
// Funzione per la creazione del giocatore
public void CreaGiocatore()
{
Application.LoadLevel(1);
DontDestroyOnLoad(this);
Instantiate(playerCharacter);
}
}
What do you think is wrong in this?