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 /
avatar image
0
Question by Gilles_aerts · Mar 04, 2019 at 12:32 AM · enumconstructorconstructors

error with a constructor

Hi i'm trying to make a deck of cards but ran into a problem with my PlayCard constructor :

   public class PlayingCard : MonoBehaviour
  {
 private enum Suits {Diamond,Heart, Club, Spade }; 
 private enum Color {Black, Red};
 private int _value;
 private Suits _suit;
 private Color _color;

 private void Start()
 {
     _suit = Suits.Diamond;
      
         
 } // there seems to be an error with this constructor, any idea's ?
 public PlayingCard(int value, Suits suit, Color color) 
 {
     _value = value;
     _suit = suit;
     _color = color;
 }


}

Comment
Add comment · Show 1
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 Cornelis-de-Jager · Mar 04, 2019 at 12:43 AM 0
Share

Can't see anything wrong with it. Seems fine to me.

2 Replies

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

Answer by WarmedxMints · Mar 04, 2019 at 12:45 AM

You can't have a constructor on a monobehavior. You can't use the new keyword to create one anyway, that's what addcomponent is for. To do what you want there, just make a void that does it. For example, if you had this in your PlayingCard Class;


  public void Init(int value, Suits suit, Color color) 
  {
      _value = value;
      _suit = suit;
      _color = color;
  }



When you add the playingcard class to a gameobject you would do;


 var go = Instatiate(prefab);
 
 var playingCard = go.AddComponent<PlayingCard>();
 
 playingCard.Init(myIntValue, mySuit, myColour);
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 Bunny83 · Mar 04, 2019 at 04:17 AM 0
Share

While most you said is correct, please don't call "a method" "a void". You don't say i'm driving "a blue". I'm driving a car which might happen to be blue but it's still a car. "void" is just the return type of the method (just like blue is only the color of the car).


I've seen this slaughter of common terms a lot recently. Besides methods without a valid return value (the return type is "void") there are also void pointers. So using "void" as a synonym for methods is not only ambiguous but also misleading and just wrong (just like the color blue should not be used as a synonym for a car). Strangely this missuse seem to only apply to the pseudo type void. Nobody calls a method that returns an integer "an int".

avatar image WarmedxMints Bunny83 · Mar 04, 2019 at 04:57 AM 0
Share

You need to get out more :P

avatar image
1

Answer by xxmariofer · Mar 04, 2019 at 12:45 AM

you cant use Constructors with monobehaviour classes, since are classes that must have an object attached to. you need to use the start awake for initializing your monobehaviour objects. if you dont want it to be atached to an object remove the monobehaviour.

Comment
Add comment · 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

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

104 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

Related Questions

Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer

Using an enum in a class constructor. 1 Answer

Why is the value of my enum being reset outside of my class constructor? 0 Answers

Inconsistent Accessibility: Parameter type is less accessible than method 1 Answer

Construct class with enum parameter (javascript) 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