Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Missing an "object reference" for another script's variable

Discussion in 'Scripting' started by Swimmtrunk, Jul 7, 2021.

  1. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    I've been working on a script that functions as a pick-up system for a player to unlock new abilities. In this case, the ability is to double jump. My problem is within trying to increase the variable I use to adjust how many jumps the player can use: doubleJumpCountMax.

    I can include the script working with this one if needed, but this is the one I'm working on that currently holds the problem:

    Code (CSharp):
    1. public class ItemPickup : MonoBehaviour
    2. {
    3.     //Used for checking if specifically *player* picking up/colliding with item
    4.     [SerializeField] private LayerMask playerColliding;
    5.  
    6.     public GameObject Player; //Assigned in the Unity UI (Drag n' dropped the player object into this field)
    7.     private CircleCollider2D circleColl;
    8.  
    9.     //Used to reference PlayerMovement script, which holds the double jump mechanic
    10.     PlayerMovement playerMovementScript;
    11.  
    12.     void Start()
    13.     {
    14.         //Assigns circleColl with the collider of the current item object
    15.         circleColl = GetComponent<CircleCollider2D>();
    16.     }
    17.  
    18.     // !PROBLEM! Supposed to increase the max amount of jumps the player has to "unlock" the double jump ability
    19.     void FixedUpdate()
    20.     {
    21.         if (IsGrabbed())
    22.             PlayerMovement.doubleJumpCountMax = 1;
    23.             //"doubleJumpCountMax" is a variable from the PlayerMovement script to limit how many jumps the player can use
    24.     }
    25.  
    26.     //Checks to see if the player has picked up the item with a boxcast showing if it has collided with the player
    27.    //(which involves the CircleCollider2D's bounds as the origin and size of the boxcast)
    28.     private bool IsGrabbed()
    29.     {
    30.         return Physics2D.BoxCast(circleColl.bounds.center, circleColl.bounds.size, 0f, Vector2.left, .5f, playerColliding);
    31.     }
    32. }
    Now I'm just met with the error message of:
    error CS0120: An object reference is required for the non-static field, method, or property 'PlayerMovement.doubleJumpCountMax' even though I wouldn't expect there to be an object required for the variable that is already defined as just an int

    I tried to leave notes to help explain the process a bit more, hope it helps!

    I appreciate any feedback!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Get rid of:
    Code (CSharp):
    1. public GameObject Player; //Assigned in the Unity UI (Drag n' dropped the player object into this field)
    2.  
    instead, make this one public:
    Code (CSharp):
    1. //Used to reference PlayerMovement script, which holds the double jump mechanic
    2. public PlayerMovement playerMovementScript;
    Now do your drag and drop thing in the editor for that field instead. And then to use it:
    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3.     if (IsGrabbed())
    4.         playerMovementScript.doubleJumpCountMax = 1;
    5.         //"doubleJumpCountMax" is a variable from the PlayerMovement script to limit how many jumps the player can use
    6. }
    There's rarely a good reason to reference the GameObject itself unless you want to just call SetActive() on it. Otherwise it's simpler to reference a component directly.
     
    Swimmtrunk likes this.
  3. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    Got it, I hadn't considered grabbing the script directly for some reason. Thank you!
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    It's still possible when you grab the GameObject - you'd just have to call GetComponent on it to get your actual script instance. Your main problem was:
    Code (CSharp):
    1. PlayerMovement.doubleJumpCountMax = 1;
    Because you're trying to use the class name "PlayerMovement" instead of an actual variable of type PlayerMovement. You need a specific instance of the class to do things with, otherwise the code doesn't know which PlayerMovement you mean.