new vs addcomponent unity

Updated on May 18, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on May 18, 2017

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Deck : MonoBehaviour
{

public int minVal = 1;
public int maxVal = 13;
List<Card>
_cards = new List<Card>();
public void Awake()
{

foreach (var Suit in Enum.GetValues(typeof(Card.Suit)).Cast<Card.Suit>())
{
for (int i = minVal; i <= maxVal; i++)
{
_cards.Add(new Card(Suit, i));
}
}
}

}

referencing
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Card : MonoBehaviour
{
public enum Suit { Clubs, Hearts, Diomonds, Spades };

public Suit suit;

public int faceValue;

public Card(Suit _suit, int _faceValue)
{
suit = _suit;
faceValue = _faceValue;
}

}
I end up with an error in unity stating that new is not allowed and i need to use add component but the unity forum answer does not work or at least i do not understand it well enough to use it.

  • Liked by
Reply