- Home /
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();
}
}
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.
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);
}
}
Your answer
Follow this Question
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