Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by masteproofgamel · Jun 07, 2016 at 01:20 PM · c#script.rpgstats

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ownerfate · Jun 07, 2016 at 01:34 PM 0
Share

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.

avatar image masteproofgamel ownerfate · Jun 07, 2016 at 01:39 PM 0
Share

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

avatar image ownerfate masteproofgamel · Jun 07, 2016 at 01:42 PM 0
Share

.ah, okay.

avatar image ben-rasooli · Jun 07, 2016 at 01:42 PM 0
Share

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.

avatar image masteproofgamel ben-rasooli · Jun 07, 2016 at 01:46 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image masteproofgamel · Jun 08, 2016 at 08:40 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges