RPG instatiating the player
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?
i don't work with C# but you would use the " AddComponent " method along with a tag.
this is an example on how to add scripts to objects that were instantiated. so it would look like this in Js:
var player : GameObject;
function Update (){
player = GameObject.FindWithTag("Player");
}
function On$$anonymous$$ouseDown (){
player.AddComponent(Script_Name_Here);
}
this would be how you would add scripts to gameObjects that were instantiated into your game.
The problem is that I set the create function in the same script where I set the player stats. For example in the script I said player.Strength = player.Class.Strength
What does actually happen when you run this script? I'm guessing when you load a level, this script gets destroyed and never make it to the next line which is Instantiate(playerCharacter);
.
So if you could say what you are looking for and what kind of stats do you mean, I might be able to help?
As a side note, Application.LoadLevel()
is Obsolete, use Scene$$anonymous$$anager.LoadScene()
ins$$anonymous$$d.
http://answers.unity3d.com/questions/1197427/rpg-class-selection-menu.html#comment-1198437
There, there is the script with the player stats. And as I said before the CreatePlayer function is wrote in this script.
I want to give those stats to the character instantiated.
Answer by ben-rasooli · Jun 07, 2016 at 02:58 PM
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;
}
Answer by masteproofgamel · Jun 08, 2016 at 08:38 AM
@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?
I didn't set yet the mage's model because I'm still searching one. And I want to set the character's model that it is the same of the class selected. For example if the warrior class is true and I click the create button I will instantiate the warrior's model with his stats.
Your answer
Follow this Question
Related Questions
RPG Class Selection Menu 2 Answers
How to save player1's stats separate from player2's i.e. Health, Damage, Etc.. 1 Answer
Smoothly multiply float 0 Answers
Roll a Ball game. Ball Rolling Diagonally 0 Answers
Scripting Help desperately needed. 1 Answer