Deck Scripting problem
Okay so, this is a small problem. I am having a hard time with a line of code from a help on here. The tip is from 2012 and it is only giving me a problem with the line 25: _card = Instantiate(asst, position, rotation) What is going that I don't get? it is telling me I cannot change an Object to a GameObject.
public enum SuitEnum {
Hearts = 1,
Clubs = 2,
Diamonds = 3,
Spades = 4,
}
public class Card {
private SuitEnum _suit;
private int _rank;
public SuitEnum Suit { get { return _suit; } }
public int Rank { get { return _rank; } }
private GameObject _card;
public Card(SuitEnum suit, int rank, Vector3 position, Quaternion rotation) {
// to do: validate rank, position, and rotation
string assetName = string.Format("Card_{0}_{1}", suit, rank); // Example: "Card_1_10" would be the Jack of Hearts.
GameObject asset = GameObject.Find(assetName);
if (asset == null) {
Debug.LogError("Asset '" + assetName + "' could not be found.");
}
else {
_card = Instantiate(asset, position, rotation);
_suit = suit;
_rank = rank;
}
}
}
public class Deck {
private List<Card> _deck = new List<Card>();
private List<Card> _discardPile = new List<Card>();
public void Shuffle() {
/* To Do */
}
public Card TakeCard() {
if (_deck.Count == 0)
return null; // the deck is depleted
// take the first card off the deck and add it to the discard pile
Card card = _deck[0];
_deck.RemoveAt(0);
_discardPile.Add(card);
return card;
}
/* ...etc... */
}
Answer by Rothanan · Jul 26, 2016 at 06:53 PM
All you're missing is a cast. Try adding "as GameObject", something like this:
else {
_card = Instantiate(asset, position, rotation) as GameObject;
_suit = suit;
_rank = rank;
}
Answer by NoseKills · Jul 26, 2016 at 03:56 PM
You are calling Instantiate but your class is not a MonoBehaviour. There is no Instantiate method in this class. You should be getting an error about that. The error you mention might be because the compiler doesn't know what that non-existing method should return so it assumes it's a System.Object.
I think you should be calling UnityEngine.Object.Instantiate
and casting that to a GameObject or you should make this class extend MonoBehaviour
it's all in a stander class public name : $$anonymous$$onoBeaviour
Your answer
Follow this Question
Related Questions
Anyone wanna help? 0 Answers
RequireComponent not adding in components 1 Answer
FixedUpdate logic step by step 1 Answer
How can I add an article to a noun in this C# code? 0 Answers
Editor crashes immediately 0 Answers