0

I have a static Player class that I have a Jump method in it. It takes a Rigidbody2D parameter so I have to call it by typing

Player.Jump(GetComponent<Rigidbody2D>);

but I want to get the Rigidbody2D component in the Jump method and call it by typing

Player.Jump();

Is there any way to do this? Can I get the Rigidbody2D component from the Jump method?

My Jump code:

    /// <summary>
    /// Makes the given Rigidbody2D jump.
    /// </summary>
    public static void Jump(Rigidbody2D rb)
    {
        rb.velocity = new Vector2(rb.velocity.x, 0);
        rb.AddForce(new Vector2(0, JumpHeight));
    }

Class that I'm using the Jump method

if (Player.CanMove)
    {
        Player.Move(rb);

        if (Input.GetKeyDown(KeyCode.Space) && canJump)
        {
            if (tempMaxJumps > 0)
            {
                Player.Jump(rb);
                tempMaxJumps--;
            }
            else
            {
                canJump = false;
            }
        }
    }
  • What does your class look like? Is GetComponent accessible inside the Jump method? – nixkuroi Jul 09 '18 at 19:35
  • My player class has just some basic data like Health or Attack and methods like Jump or Move, nothing else. GetComponent is a method made by Unity and I don't have access to it. I can call it by using MonoBehaviour - Unity made class again- but I can't create a new MonoBehaviour(), I have to Inherit in order to use it and in order to my code run, I must not inherit from MonoBehaviour in the player class –  Jul 09 '18 at 19:37
  • Can you make your MonoBehavior into an application constant in your App.xaml.cs or global.asax.cs (or whatever the main file is) and the access it like ((App)Application.Current).Behavior.GetComponent() ? – nixkuroi Jul 09 '18 at 19:45
  • a Player-class feels like a script that should inherit from MonoBehaviour – Fredrik Widerberg Jul 09 '18 at 19:45
  • Unfortunately, static classes can not derive from MonoBehaviour :( –  Jul 09 '18 at 19:50

3 Answers3

3

If you really need the player class to stay a static class, i suggest simply adding a

public static RigidBody2D Rigidbody;

to which you can assign to once in the Start of the class calling Jump

However this whole question seems like bad practice, a Player script should generally be inherit from MonoBehaviour and be attached to the player GameObject in your scene. Then inside this Player script you can check for input and have a Jump-method

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
  • 4
    Static classes are a smell of someone that does not understand OOP ... lets code it using C – Ignacio Soler Garcia Jul 09 '18 at 19:54
  • Thanks so much, I've never thought about this :) But I want only one player in my whole game so I made the player class static.(Also the time I wrote that code, I had no idea about singletons) –  Jul 09 '18 at 19:54
  • Ignacio Soler Garcia yes, I'm getting used to OOP, I don't have much experience on it :) –  Jul 09 '18 at 19:55
  • I'm with @IgnacioSolerGarcia, one instance doesn't mean its OK to make it a static class. A great example of a static class is something like `Math`, where you only need methods / properties, and you don't store values (like Player's position, number of jumps left, etc.) – Ivan García Topete Jul 09 '18 at 19:57
  • 2
    If you need a single instance you don't need a singleton (they suck too). Just create a single instance of the class :) – Ignacio Soler Garcia Jul 09 '18 at 19:58
  • 1
    You should look at [singleton pattern](https://stackoverflow.com/questions/2155688/what-is-a-singleton-in-c) or [dependency injection](https://stackoverflow.com/questions/14301389/why-does-one-use-dependency-injection) – Ivan García Topete Jul 09 '18 at 19:59
3

You could create a static member which would have a reference to the rigidbody and set the reference at some point prior to calling Jump, but that's a bad idea.

By using the static class to make the player object jump, you are breaking abstraction. Now you have code outside of your player object that is influencing the player, leading to code spaghetti.

Fix
  • 198
  • 1
  • 12
  • 2
    Agreed, `Player` class **should be** an instance of an object. The question is a [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Ivan García Topete Jul 09 '18 at 20:03
0

Like this?

public static class Player
{
    public static Rigidbody2D Jump()
    {
        return new Rigidbody2D();
    }
}
J. Doe
  • 31
  • 4
  • I can not create a new Rigidbody2D, I should use the one attached to the class that I'm using the Jump method in –  Jul 09 '18 at 19:44
  • This answer makes no sense. – Draco18s no longer trusts SE Jul 09 '18 at 20:38
  • Not sure but doesn't it made sense before editing? "I have a static Player class that I have a Jump method in it. It takes a Rigidbody2D parameter so I have to call it by typing Player.Jump(GetComponent); but I want to get the Rigidbody2D component in the Jump method and call it by typing Player.Jump(); Is there any way to do this? Can I get the Rigidbody2D component from the Jump method?" – J. Doe Jul 09 '18 at 20:43