Pookie
Pookie

Reputation: 1279

Class to control two classes in Unity

I have almost identical classes, PaddleLeft and PaddleRight. I am tired of calling both of those classes when I need something done, I rather them be done all at once. For example, here is what I have to do:

public void pause() {
    GameObject.Find("Paddle Objects/paddleRight").GetComponent<Paddle>().setIsPaused(true);
    GameObject.Find("Paddle Objects/paddleLeft").GetComponent<Paddle>().setIsPaused(true);
}

And here is what I want to do:

public void pause() {
    GameObject.Find("Paddle Objects/paddles").GetComponent<Paddle>().setIsPaused(true);
}

This seems unnecessary, however, in my game, there are times where the same line of code are copied and adjusted to up to ten similar objects.

Is there a way to make a super class like in Java for these objects. I don't understand how to make it work because I can't extend MonoBehavior and a superclass in Unity.

Upvotes: 0

Views: 267

Answers (3)

Programmer
Programmer

Reputation: 125255

I have almost identical classes, PaddleLeft and PaddleRight

But your code is totally saying different stuff

 GameObject.Find("Paddle Objects/paddleRight").GetComponent<Paddle>().setIsPaused(true);
 GameObject.Find("Paddle Objects/paddleLeft").GetComponent<Paddle>().setIsPaused(true);

Maybe you want meant to write the code below?

 GameObject.Find("Paddle Objects/paddleRight").GetComponent<PaddleRight>().setIsPaused(true);
 GameObject.Find("Paddle Objects/paddleLeft").GetComponent<PaddleLeft>().setIsPaused(true);

I will assume the second code is what you meant to write.

When you have multiple GameObjects or Scripts with similar actions, you should create a central manager script that will make it easy to communicate with a those GameObjects or classes.

Since both your classes are called PaddleRight and PaddleLeft, You can simply call this class PaddleManager.

Don't forget that, of course, PaddleManager is just a script, it's not a "thing" in Unity. Naturally you will attach PaddleManager to some game object. You might ask "where should I attach it?" In a simple game, you might attach it to your camera, say. (Since you always have a camera, other developers working on your project always know to "look n the camera" for odds and ends like sound-effects, managers like this and so on.) Alternately, say that physically all your paddles are associated with (for example) an object that is the ping pong table. Then, a good place to attach PaddleManager.cs would be on the ping pong table. It doesn't matter where you attach it, so long as it is tidy. Some people like to make simply an empty object (you can refer to an empty object as a "marker"), just make an empty object named say "manager holder", put it at 0,0,0, and you can add scripts like PaddleManager.cs to that object.

Your PaddleManager.cs script:

public class PaddleManager : MonoBehaviour
{
   private PaddleRight rightPaddle = null;
   private PaddleLeft  leftPaddle = null;

    //Initialize variables
    void Start()
    {
        //Get reference/Cache
        rightPaddle =  GameObject.Find("Paddle Objects/paddleRight").GetComponent<PaddleRight>();

        //Get reference/Cache
        leftPaddle =  GameObject.Find("Paddle Objects/paddleLeft").GetComponent<PaddleLeft>();
    }

    //Call to pause and unpause
    public void pause(bool pausePaddle)
    {
        rightPaddle.setIsPaused(pausePaddle);
        leftPaddle.setIsPaused(pausePaddle);
    }
}

Now, you can access both of your Paddles from one script, in another script.

public class YourOtherScript : MonoBehaviour{

    PaddleManager paddleManager = null;

    void Start()
     {
         //Get reference/Cache
         paddleManager = GameObject.Find("GameObjectPaddleManaerIsAttchedTo") .GetComponent<PaddleManager>();

         //To pause
         paddleManager.pause(true);

         //To un-pause
         paddleManager.pause(false);
     }
}

By doing this, you will avoid using static variable and also avoid using GameObject.FindGameObjectsWithTag("paddles")) in foreach loop like mentioned in the other answer. GameObject.Find... functions should NOT be used in the middle of the game because it will slow down your game. You need to use it once and cache the GameObject in the Start function, then you can re-use it without slowing down your game.

Upvotes: 4

LumbusterTick
LumbusterTick

Reputation: 1075

First of all paddle right and left are game-objects and not classes , the class name is paddle , and if the same script is on both the objects , the most simplest way would be to put it on another empty object and call it once and the code will work on all game-objects that the script is attached to . BUT! that is only regarding to what I cuold make out of your question , Here is what I really recommend , as you said "because I can't extend Mono-behavior and a super-class in Unity" . Ok so you have class A inheriting monobehavior , and you make class's B and C, then when you inherit them from A you will get all the abilities of a monobehaviour in class B and C and you can attach them to game objects , you can even make start and update functions as vrtual and override them in B and C and you can even call A's function use the keyword Base , So read on it , it will take time but in the long run makes you a better coder

Upvotes: 1

Nika Kasradze
Nika Kasradze

Reputation: 3019

Mm, wait. There is something wrong with your question:

YouPaddleLeft and PaddleRight are not classes. They are GameObjects existing in Unity scene. Class is Paddle to which you get reference by GetComponent<Paddle>() .

Now if you have a variable/function that affects all the instances of the class the same than you shell make them static. (Google static variables and functions if you don't know what they are).

So go to your Puddle class and change the declaration of setIsPaused(bool val) to this:

public static void setIsPaused(bool val) { /* implemenetation */ }

and then make a call to it via class token, not object:

Paddle.setIsPaused(true/false);

note that if the static function has references to class variables all those variables should be marked static as well. (e.g. if you have a bool isPaused than mark it static because it should be the same for all the objects all the time anyways)

Upvotes: 0

Related Questions