Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jul 21, 2014 at 01:03 AM by Domo23000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Domo23000 · Jul 20, 2014 at 04:30 PM · listsclassesempty

Add Item To A List Of Classes

I have a List of classes, which stores info for my weapons when i switch them. I want 9 spaces for having weapons so the rest have to be filled with an icon, and and empty gameobject. how do I add an item to it in script?

using System.Collections;

using System.Collections.Generic;

public class WeaponSwitching : MonoBehaviour {

public List weapons = new List();

 [System.Serializable]
 public class WeaponList
 {
     public GameObject weapon;
     public Texture2D icon;
     public string name;
 }

 public int curWeapon;
 public Texture2D iconSelect;
 public bool allowName = true;

 public int maxItems = 9;
 public Texture2D noneIcon;
 public GameObject None;

 void Start () 
 {
     if(curWeapon > weapons.Count)
     {
         curWeapon = 0;
     }

     if(weapons.Count < 9)
     {
         weapons.AddRange{None, noneIcon, ""};__This is where i need it to be added__
     }

     weapons[curWeapon].weapon.SetActive(true);
 }

 void Update () 
 {
     float scroll = Input.GetAxis ("Mouse ScrollWheel");

     //Up
     if(scroll > 0)
     {
         weapons[curWeapon].weapon.SetActive(false);    
         curWeapon ++;
         
         if(curWeapon >= weapons.Count)
         {
             curWeapon = 0;
         }
         
         weapons[curWeapon].weapon.SetActive(true);
     }

     //Down
     if(scroll < 0)
     {
         weapons[curWeapon].weapon.SetActive(false);    
         curWeapon --;
         
         if(curWeapon < 0)
         {
             curWeapon = weapons.Count - 1;
         }
         
         weapons[curWeapon].weapon.SetActive(true);
     }
 }

 void OnGUI()
 {
     if(allowName == true)
     {
         GUI.Label(new Rect(32, Screen.height - 96, 128, 128), weapons[curWeapon].name);
     }

     for(int i = 0; i < maxItems; i ++)
     {
         GUI.DrawTexture(new Rect(64 * i + 16, 16, 64, 64), weapons[i].icon);
     }

     GUI.DrawTexture(new Rect(64 * curWeapon + 16, 16, 64, 64), iconSelect);
 }

}

Comment
Add comment · Show 2
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 ajkolenc · Jul 20, 2014 at 08:45 PM 0
Share

It looks to me that you are thinking about your "WeaponList" class in the wrong way. I think I would call that class "WeaponInfo", because it holds all the information you need about a weapon.

In line 26 it looks like you are trying to add a list to your weapons variable (that's what AddRange does), but in reality you just want to add a single object multiple times. So:

 WeaponList emptyWeapon = new Weapon();
 emptyWeapon.icon = noneIcon;
 emptyWeapon.name = "";
 
 for (int i = weapons.Count; i < 9; i++){
     weapons.Add(emptyWeapon);
 }

This fills up your remaining slots with empty weapons. I kept your class name, but it might also be a good idea to define a constructor to simply the code a bit.

avatar image Domo23000 · Jul 21, 2014 at 12:46 AM 0
Share

Thanks! @ajkolenc That'll help me a lot :)

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Trouble with Add-ing and Retrieving from a List of Custom Class 1 Answer

UNITYSCRIPT - Class's and Instances - Functions/Methods - Correct Usage 0 Answers

How to search a certain variable in a list of a created class (C#) 1 Answer

Trying to make a simple quest system, but i stuck how to do 0 Answers

Save an array with custom classes 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