• 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 /
This question was closed Mar 01, 2015 at 05:38 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Polraudio · Mar 01, 2015 at 04:29 PM · listlistsaddstruct

How to add an item to a list with a class/uJS struct?

I been trying to add a player to the list via .Add but nothing i do seems to work. So how would i add a player to that list?

 import System.Collections.Generic; 
 
 var NumOfPlayers : int;
 var PlayerTurn : boolean;
 var Player :List.<PlayerInfo>;
 
 class PlayerInfo{
     var Name : String;
     var Level : int;
     var HP : int;
     var TP : int;
     var ATK : int;
     var DEF : int;
 }
 
 function Start () {
 Player.Add("Bob", 1, 10, 5, 10, 5);
 }
 
 function OnGUI(){
 
 }
 
 function Update () {
 
 }

Thank you in advance.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by meat5000 · Mar 01, 2015 at 04:31 PM

Your class needs a constructor to build the struct. It works well in uJS to extend System.ValueType.

Notice how the function matches the classname and how the values are passed as arguments to build your PlayerInfo.

 //Making your own struct in uJS
 #pragma strict
  
 class PlayerInfo extends System.ValueType {
     var Name : String;
     var Level : int;
     var HP : int;
     var TP : int;
     var ATK : int;
     var DEF : int;
  
     public function PlayerInfo( Name : String,
                                 Level : int,
                                 HP : int,
                                 TP : int,
                                 ATK : int,
                                 DEF : int )
                                 {
                                     this.Name = Name;
                                     this.HP = HP;
                                     this.TP = TP;
                                     this.ATK = ATK;
                                     this.DEF = DEF;
                                 }
 }

 //Basic List Examples     
 #pragma strict
 import System.Collections.Generic;
 
 public class ListTest extends MonoBehaviour
 {
     public var PlayerList :List.<PlayerInfo>;
     private var paddingZero : PlayerInfo = PlayerInfo("ZeroElement", 0, 0, 0, 0, 0);
 
     function Start ()
     {
         AddPlayerInfo(paddingZero);
     }
 
     function Update()
     {
         if(Input.GetKeyDown(KeyCode.Y))
         {
             AddPlayerInfo(paddingZero);
         }
         if(Input.GetKeyDown(KeyCode.U))
         {
             ChangeInfo("Steve", 2);
         }
         if(Input.GetKeyDown(KeyCode.I))
         {
             RemoveBlock(0);
         }
     }
 
     function AddPlayerInfo(myPlayer : PlayerInfo)
     {
         this.PlayerList.Add(myPlayer);
     }
      
     public function RemoveBlock(playerIndex : int)
     {
         this.PlayerList.RemoveAt(playerIndex);
     }
 
     public function ChangeInfo(newName : String, playerID : int)
     {
         if(PlayerList[playerID] != null)
         {
             var tempHolder : PlayerInfo;
             tempHolder = this.PlayerList[playerID];
             tempHolder.Name = newName;
             this.PlayerList[playerID] = tempHolder;
         }
     }
 }


Comment

People who like this

0 Show 5 · 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 Polraudio · Mar 01, 2015 at 05:13 PM 0
Share

Ok i see how to do this now, Thank you very very much :D.

avatar image meat5000 ♦ · Mar 01, 2015 at 05:35 PM 0
Share

Most Welcome

avatar image Polraudio · Mar 02, 2015 at 05:22 AM 0
Share

Ok i ran into a problem. When i try to remove/add HP it doesn't work. Im doing Player[0].HP -=1; Displaying stuff is no problem but when i go to remove/add nothing happens. EDIT: It gives me this error on the line i have Player[0].HP -= 1; "BCW0006: WARNING: Assignment to temporary." EDIT2: May sound like a dumb question but how do i remove also? I tried Player[0].Remove(); Player[0].Remove; and Player.Remove(0);

avatar image meat5000 ♦ · Mar 02, 2015 at 09:11 PM 0
Share

Updated the code

avatar image Polraudio · Mar 02, 2015 at 09:35 PM 0
Share

AWESOME! Thanks again :D.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

can add to a list but not remove 1 Answer

Help: items not getting added to list. 1 Answer

Check if Exists and if so, Add it to Other List 1 Answer

A node in a childnode? 1 Answer

[CLOSED] When I use this code that I made, it adds to the item catalogue too? [CLOSED] 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