0

I am only new to unity and C# though I have an intermediate - advanced knowledge of python. I have learned the fundamental elements of C# and am currently trying to create a game of Pong using the Unity 2d platform. I am unsure if there is a clean and concise way for two gameObjects e.g. main_player and computer_player to inherit from a single class player with its own defined functions e.g. move()?

6
  • You mean public abstract class Player as the base class, public class MainPlayer : Player, and public class ComputerPlayer : Player? Commented Oct 16, 2017 at 11:49
  • Yes thats what I mean, for two separate game objects to inherit from a single class. Commented Oct 16, 2017 at 12:03
  • 3
    Inheritance is very basic OOP (and very basic c#) concept. If you have a specific question about how to correctly implement an inheritance tree for your game that's one thing, but this question seems to be about the inheritance concept itself. This is the kind of thing you want to learn from a good tutorial or book, not something you want to ask on StackOverflow. Commented Oct 16, 2017 at 12:07
  • Mhmm, not sure id want player to apply to both human players and so characters. Commented Oct 16, 2017 at 19:38
  • Its just very basic thing such as x pos, y pos, and a basic move function. It may not be as important for a game of Pong but for larger games it would clean up a lot of repetitive code such as animation etc. Commented Oct 16, 2017 at 23:11

2 Answers 2

1

You can go with 2 methods, abstract or interface.

public interface IMove{
    void Move();
}

public class Main_player:MonoBehaviour, IMove{
     public void Move(){}
}
public class Computer_player:MonoBehaviour, IMove{
     public void Move(){}
}

You can use GetComponent on interface but you cannot use the slots in Inspector. interface are not serializable so you cannot make them appear in editor.

For this you need abstract.

public abstract class IMove : MonoBehaviour{
    public abstract void Move();
}

public sealed class Main_player: IMove{
     public override void Move(){}
}
public sealed class Computer_player: IMove{
     public override void Move(){}
}

Whether you make Move abstract or virtual is quite similar in result. If you have a default implementation, use virtual, if you require the subclass to implement its own version, then use abstract.

More here : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract

Sign up to request clarification or add additional context in comments.

Comments

1

You can Create a class player like this.

public class Player: MonoBehaviour {
    public virtual void move(){
    }
}

Then inherit it like this:

public class Computer_player:Player{
    public override void move(){
       base.move();
       //Extra movement code goes here    
    }
}

Same way you can inherit main_player. You can find more details here: https://unity3d.com/learn/tutorials/topics/scripting/inheritance

5 Comments

No point of overriding a method if all you do is call base. simply don't write anything and let the normal inheritance work.
It's better to mark move()-method in Computer_player as override. Your syntax is similar to "new void move()", so it hide base method and will produce related warning. It is not good. So full declaration syntax for "move" method must be "public override move()"
That not exactly my problem, lets say computer_player and main_player are in two separate scripts how would they inherit from the same abstract class Player.
Also, if your Computer_player is the bottom line of the inheritance, add the sealed keyword. It makes compiler go faster. When running a virtual method, the compiler needs to check the virtual table, if the method is sealed, unity is able to jump directly to the final one as it knows there cannot be anything below.
Oops, forgot to put the override keyword. In case of Abstract class, @Everts has given a better solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.