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 Alengthyname · Jun 05, 2016 at 04:10 PM · uiarraypanel

Panel with Values From Array

Hello,

I'm attempting to create a character stats screen for a tactical RPG type game where you can look through all of your units and see their stats in one page.

I have the roster of the characters as below:

  public class Roster : MonoBehaviour {
  
 public int characters;
 public BaseCharacter[] characterRoster;
   
 void Start () {
     characterRoster = new BaseCharacter[characters];
     
     for (int i = 0; i < characters; i++)
     {          
         characterRoster[i] = new BaseCharacter();
          //defining stats
         characterRoster[i].characterName = "name:" + i.ToString(); 
       }       
 }

 }

and I'd like to make it so that a panel gets a child panel for each character in the roster with the various character variables I have and will be adding in:

 public class BaseCharacter : MonoBehaviour {

    public string characterName;
 }

If anyone could help, I'd greatly appreciate it. Thank you for your time.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Jun 05, 2016 at 07:30 PM

First you cannot do new BaseCharacter(); because BaseCharacter is a MonoBehaviour class which is not allowed.

So you will either need to create a separate character data class which is used by the BaseCharacter class or make the BaseCharacter class a generic class.

You can however do this

 public class CharacterData
 {
     public string characterName;
     // add any other character stats here
     // e.g.
     public int strength;
     public int dexterity;
     public int intelligence;
     public int resistance;
     public int vitality;
     public int criticalHitChance;
     public int criticalHitDamage;
 }
 
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Roster : MonoBehaviour
 {
     public int characters;
     public List<CharacterData> characterRoster = new List<CharacterData>();
 
     public GameObject parentCanvas;
     public GameObject characterPanelPrefab;
 
     void Start ()
     {
         if (parentCanvas == null)
         {
             Debug.LogError("parentCanvas is not set in the inspector. Please set before continuing");
         }
         if (characterPanelPrefab == null)
         {
             Debug.LogError("characterPanelPrefab is not set in the inspector. Please set before continuing");
         }
         for (int i = 0; i < characters; i++)
         {          
             CharacterData character = new CharacterData();
             character.characterName = "name:" + i.ToString(); 
             
             // add any other character stats here
             // e.g.
             strength = (int)Random.Range(20, 50);
             dexterity = (int)Random.Range(20, 50);
             intelligence = (int)Random.Range(20, 50);
             resistance = (int)Random.Range(20, 50);
             vitality = (int)Random.Range(20, 50);
             criticalHitChance = (int)Random.Range(20, 40);
             criticalHitDamage = (int)Random.Range(20, 40);
 
             characterRoster.Add(character);
             AddCharacterPanel(character);
         }
     }
 
     void AddCharacterPanel(CharacterData character)
     {
         GameObject spawnedObj = Instantiate(characterPanelPrefab);
 
         // make the spawned panel a child of the parentCanvas
         spawnedObj.transform.SetParent(parentCanvas.transform, false);
         
         // you need to do this for each item in your CharacterData class that you want on the panel
         // e.g.
         
         // get a GameObject named "Name" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("Name"))
         {
             spawnedObj.transform.Find("Name").GetComponent<Text>() = character.characterName;
         }
 
         // get a GameObject named "Strength" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("Strength"))
         {
             spawnedObj.transform.Find("Strength").GetComponent<Text>() = character.strength;
         }
 
         // get a GameObject named "Dexterity" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("Dexterity"))
         {
             spawnedObj.transform.Find("Dexterity").GetComponent<Text>() = character.dexterity;
         }
 
         // get a GameObject named "Intelligence" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("Intelligence"))
         {
             spawnedObj.transform.Find("Intelligence").GetComponent<Text>() = character.intelligence;
         }
 
         // get a GameObject named "Resistance" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("Resistance"))
         {
             spawnedObj.transform.Find("Resistance").GetComponent<Text>() = character.resistance;
         }
 
         // get a GameObject named "Vitality" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("Vitality"))
         {
             spawnedObj.transform.Find("Vitality").GetComponent<Text>() = character.vitality;
         }
 
         // get a GameObject named "CriticalHitChance" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("CriticalHitChance"))
         {
             spawnedObj.transform.Find("CriticalHitChance").GetComponent<Text>() = character.criticalHitChance;
         }
 
         // get a GameObject named "CriticalHitDamage" under the spawned prefab GameObject (must have a Text component attached)
         if (spawnedObj.transform.Find("CriticalHitDamage"))
         {
             spawnedObj.transform.Find("CriticalHitDamage").GetComponent<Text>() = character.criticalHitDamage;
         }
     }
 }

Depending on your panel structure tou will need to create a UI prefab something like the one listed below (based on the data above)

 UI Panel Object
     UI Text Object - name: "Name"
     UI Text Object - name: "Strength"
     UI Text Object - name: "Dexterity"
     UI Text Object - name: "Intelligence"
     UI Text Object - name: "Resistance"
     UI Text Object - name: "Vitality"
     UI Text Object - name: "CriticalHitChance"
     UI Text Object - name: "CriticalHitDamage"
Comment
Add comment · Show 3 · 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 Alengthyname · Jun 06, 2016 at 05:36 PM 0
Share

Thank you, I appreciate the response! I wound up having something similar to what you outlined just working through it myself; having a function to instantiate prefabs and parent them under my roster panel in the scene. It's reassuring!

I was a little concerned about Unity throwing warnings about how I wasn't able to use the new keyword for $$anonymous$$onobehaviour derived scripts, but it worked so I sort of just ignored the warning. Should I have my BaseCharacter class derive from some CharacterData class? I attempted this and got a bunch of errors saying that the stats did not exist.

I haven't worked much with lists (just some pathfinding tutorials using them for node grids), but I'm quite familiar with arrays. Would using a list to store the roster be more advantageous than using arrays?

avatar image TBruce Alengthyname · Jun 06, 2016 at 06:09 PM 1
Share

Yes Unity will run even when you do a new on a $$anonymous$$onobehaviour script but this is not allowed because $$anonymous$$onobehaviour scripts have to be attached to GameComponents.

Basically as I outlined above, and for simplicity, you only need one prefab for your panel. And on that prefab have several child GameOIbjects to meet your needs.

Lists are basically the same as arrays but Lists have more functionality and are preferred in C#. But to answer your question I would personally say yes especially since resizing an array is more difficult than a list is. To remove an item from a list it is easy as

 myList.RemoveAt(n);

where it is not so simple with an array.

Please click the tick to accept the answer if your question was answered.

avatar image Alengthyname TBruce · Jun 07, 2016 at 07:52 PM 0
Share

I'll go ahead and try setting up the roster with a list then, that does seem a lot easier to work with. Also, it's really encouraging when you're new to Unity and game development in general when people help with questions, so thanks again for that.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Activate and deactivate panels with buttons 3 Answers

Move UI panel on and off screen, scripted without animation 0 Answers

Screen Format Question (Panels) 1 Answer

How to find the position of a button onClick? (array) 0 Answers

how do I Instantiate gameobject and add to array and then move the previous instantiated gameobject above the current? 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