Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by HannibalMaverick · Jul 26, 2016 at 03:37 PM · c#programmingerror build

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... */
     }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
                  }

Comment
Add comment · Show 1 · 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 HannibalMaverick · Jul 27, 2016 at 02:45 AM 1
Share

Thanks, that really helped.

avatar image
1

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

Comment
Add comment · Show 1 · 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 HannibalMaverick · Jul 26, 2016 at 05:15 PM 0
Share

it's all in a stander class public name : $$anonymous$$onoBeaviour

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

210 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

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


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