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 /
avatar image
-1
Question by unifan · Jan 23, 2015 at 10:57 AM · c#arrayaddcomponentoop

C# how to add custom class object to array

I made a custom class named card, in another custom class named Deck I want to add Card objects to an array. in the deck class function filldeck() my logic seems fine but in the inspector it shows each array item noneType it tells me to use Addcomponent function, not sure how. can any1 help.

here the code :

Card class:


using UnityEngine; using System.Collections;

public class Card: MonoBehaviour{

     public Sprite sprite;
     public string rank;
     public string color;
     
     public Card(Sprite mySprite, string myRank, string myColor){
         sprite = mySprite;
         rank= myRank;
         color= myColor;

         
     }
     
     
     
 }

Deck Class:

using UnityEngine; using System.Collections; using System.Collections.Generic; //"clubs", "spades", " hearts", "diamonds" //"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king" public class Deck :MonoBehaviour {

 public Card[] cards; 
 private string[] colors = new string[] {"clubs", "spades", "hearts", "diamonds"};
 private string[] ranks = new string[]{"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};
 private int currentRank=1;

 public void fillDeck(){

     for(int i =0; i<52; i++){
                 
         cards= new Card[52];

             if(i<13&&i>=0){
                 Card tempCard= new Card(Resources.Load<Sprite>("image/deck_"+i),ranks[currentRank],colors[0]);
                 cards[i]=tempCard;
                 
                 currentRank+=1;
                 
                 if(currentRank>=13){
                     currentRank=1;
                 }
             }

             
     }
         

 }

 public void Start(){


     this.fillDeck();
     

 }

}

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
2

Answer by moonstruck · Jan 23, 2015 at 11:27 AM

First of all, don't use constructors and creation using new if your class inherits MonoBehaviour. Replace constructor with some Initialize function or perform it in Start. Instead of new use AddComponent() or Instantiate(prefabName) where prefabName is a card prefab. I suggest the latter cause probably you will need to move each card independently.

Comment
Add comment · Show 2 · 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 unifan · Jan 24, 2015 at 06:54 AM 0
Share

so ive change things around but it still only adds none(gameobject) to my List


using UnityEngine; using System.Collections; using System.Collections.Generic; //"clubs", "spades", " hearts", "diamonds" //"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king" public class Deck :$$anonymous$$onoBehaviour {

 public List<GameObject> cards; 
 private string[] colors = new string[] {"Clubs", "Spades", "Hearts", "Diamonds"};
 private string[] ranks = new string[]{"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};
 private int currentRank=0;

 public void fillDeck(){
     GameObject obj = null;
     //shuffler= gameObject.AddComponent<>();
     for(int i =0; i<52; i++){
                 
             

             if(i<13&&i>=0){
                 
                 obj= Resources.Load<GameObject>("prefabs/"+ranks[currentRank]+"Of"+colors[0]) as GameObject;
                 cards.Add(obj);
                 
                 

                 currentRank+=1;
                 
                 if(currentRank>13){
                     currentRank=1;
                 }
             }

             
     }
         

 }

 public void Start(){


     this.fillDeck();
     //Instantiate (cards [0].sprite, new Vector3 (0, 0, 0), Quaternion.identity);

 }

}


avatar image getyour411 · Jan 24, 2015 at 06:55 AM 0
Share

Use the 1010 button to format your code properly

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Trying to access points[0].position of type Transform. 1 Answer

Array Issue 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