Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 madmike6537 · Jan 23, 2013 at 03:57 AM · inheritance

Stuck when using inheritance(C#)

I am not sure why but I always struggle with figuring this basic scripting piece out. I am trying to use a Parent and Child class to make my code cleaner and easier to read and work with. So I set a base class with Getters and Setters:

 using UnityEngine;
 using System.Collections;
 
 public class BaseSkill : MonoBehaviour
 {
     //These are the different aspects of a skill
     private string _name;
     private int _maxPoints;
     private int _curPoints;
     private string _description;
 
     #region Getters and Setters
     public string Name
     {
         get{return _name;}
         set{_name = value;}
     }
     
     public string MaxPoints
     {
         get{return _maxPoints;}
         set{_maxPoints = value;}
     }
     
     public string CurPoints
     {
         get{return _curPoints;}
         set{_curPoints = value;}
     }
 
     public string Description
     {
         get{return _description;}
         set{_description = value;}
     }
     #endregion
     
     
 }

And then I inherit from it:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 /// <summary>
 /// This script is attached to the multiplayerManager and keeps track of what skills the player has available in a list.
 /// Skills are kept track of by a point system. Each player has a certain amount of skill points to spend. If they spend a point
 /// in a certain skill they gain the function of that skill. 
 /// </summary>
 /// 
 public class PlayerSkills : BaseSkill 
 {
     //Skill points player can spend
     public int skillPoints;
     
     //List of Player Skills here
     public List<BaseSkill> MySkills = new List<BaseSkill>();
     
     //Create the first skill
     BaseSkill SwiftFeet = new BaseSkill();
     SwiftFeet.Name = "SwiftFeet";
 
 }

But.. On the line SwiftFeet.Name = "SwiftFeet"; I get this error:

error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration

What am I doing wrong and how can I set those base variables through my getters and setters for my newly created skill? As you can probably see I am trying to create skills that all have similar attibutes, a name, description, maxPoints, etc. I need to set these variables for each skill and then I want to add these into a list for the player. Thanks in advance.

Comment
Add comment · Show 4
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 Ellandar · Feb 13, 2013 at 04:05 AM 0
Share

G'day madmike, I hope this worked out for you (and I know it was a week or two ago you asked), but reading your code re$$anonymous$$ded me of a tutorial I saw briefly a while ago. He was approaching the problem the same way, this guy treated each "BaseSkill" as a single skill. These base skills were then added to another class called "PC", as a list there.

This might be what you are after if you are looking for information on this topic: http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial at the very least it should give you a heap of pointers for other components of your game too.

avatar image madmike6537 · Feb 13, 2013 at 03:01 PM 0
Share

Thanks. Yeah those tutorials are great I check them out frequently to get ideas. Thanks for the reply!

avatar image fafase · Feb 13, 2013 at 03:36 PM 0
Share

Personal point of view but this:

 public string $$anonymous$$axPoints
 {
 get{return _maxPoints;}
 set{_maxPoints = value;}
 }

seems pretty useless to me as it does not do anything better than a simple public variable.

This on the other hand does something:

 public string $$anonymous$$axPoints
 {
    get{return _maxPoints;}
    set{
       if(value < int.$$anonymous$$axValue && value > -1)
          _maxPoints = value;}
 }

As it checks if the value will not do anything wrong and hence legitimate the setter.

Now, if there is any underlying reason as why to use properties when nothing else than get;set; is done please enlighten me as I really do not see the point of it.

avatar image Ellandar · Feb 14, 2013 at 08:35 AM 0
Share

Like you mentioned fafase, there isn't really a need to do it if you are not modifying the getter and setter, but I went through a similar process earlier this month (should I, shouldn't I?) and these were my reasons:

Reasons for:

  • Compliance to the .NET coding standards.

  • Compliance to your own format standards.

  • You intend on applying controls around how get/set works in the future.

The reasons against it:

  • properties do not show automatically in the inspector, you need to create a custom inspector to expose the information.

  • extra code to maintain.

  • Compliance to your own format standards.

Reasons that shouldn't factor into the decision:

  • performance (a few other articles have shown there is very little performance difference due to the JIT compiler) References below.

  • Because someone else thinks it's wrong, this is along the lines of using underscores in front of private variables.

In the end, I believe it's down to preference.

Tip. If you think it takes up too much room, you could also use this format to cull it down a bit:

 public String AutoProperty { get; private set; }

my 2c :)

Edit References: http://stackoverflow.com/questions/646779/does-c-sharp-inline-properties/646780#646780

Edit 2: I went with "don't use them unless constraining or trying to control the setting of fields". Although I did extend the inspector to allow me to set/view properties, I really didn't like how much maintenance the code was taking just to maintain the use of properties.

Rob.

2 Replies

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

Answer by kru · Jan 23, 2013 at 04:01 AM

You can't assign an instance property outside of a method. Put the line where you set SwiftFeet.Name into the Start() or Awake() method of your script.

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 madmike6537 · Jan 23, 2013 at 04:04 AM 0
Share

Ah comon was it really that simple lol. Thanks.

avatar image
0

Answer by temptest123 · Jan 23, 2013 at 04:10 AM

Are you sure you want to use Inheritance here?

You are aware that your PlayerSkills now IS a BaseSkill (through inheritance) and also HAS BaseSkills (in the MySkills list)?

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 madmike6537 · Jan 23, 2013 at 04:15 AM 0
Share

Hmm that's true. $$anonymous$$aybe I need to rethink this set up. Still learning scripting. Thought this would be cleaner but then you have a point there.

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

13 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Inheritance best solution to handle *many* instances of a class with unique values? 1 Answer

[C#] Calling a method of a child class on a parent object 1 Answer

How to serialize fields for both base and derived classes? 0 Answers

Inheritance in C# and Unity3d 1 Answer


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