Search Unity

Falling player in unity!

Discussion in 'Scripting' started by Gabimela, Jan 19, 2020.

  1. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    I am trying to get my player to die when it hits thee ground. This is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FallingPlayer : MonoBehaviour
    6. {
    7.     GameObject Player;
    8.     float CurrentFallTime;
    9.     public float MaxFallTime = 3;
    10.     bool PlayerIsFalling;
    11.     bool isGrounded;
    12.  
    13.     void Update()
    14.     {
    15.         if (Player.isGrounded == false)
    16.         {
    17.             PlayerIsFalling = true;
    18.         }
    19.         else PlayerIsFalling = false;
    20.  
    21.         if (PlayerIsFalling)
    22.         {
    23.             CurrentFallTime += Time.deltaTime;
    24.         }
    25.         if (Player.isGrounded && CurrentFallTime < MaxFallTime)
    26.         {
    27.             CurrentFallTime = 0;
    28.         }
    29.  
    30.         if (CurrentFallTime >= MaxFallTime && Player.isGrounded)
    31.         {
    32.             GameManager.Instance.FallOxygen();
    33.         }
    34.     }
    35. }

    and these are the errors i am getting:
    Assets\FallingPlayer.cs(15,20): error CS1061: 'GameObject' does not contain a definition for 'isGrounded' and no accessible extension method 'isGrounded' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)


    Assets\FallingPlayer.cs(25,20): error CS1061: 'GameObject' does not contain a definition for 'isGrounded' and no accessible extension method 'isGrounded' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)


    Assets\FallingPlayer.cs(30,54): error CS1061: 'GameObject' does not contain a definition for 'isGrounded' and no accessible extension method 'isGrounded' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
     
  2. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    You are not assigning nothing to the variable type GameObject "Player", this is why you get the error. If you want to assign the player object you have to declare "public GameObject Player" and then in the inspector you can drag your player object. Anyway i think you would get the error in that case too because a game object type doesn't have a "isGrounded" variable so you can't access to it
     
  3. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    so how would i write that?
     
  4. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    What components have you attached to the player object?
    What components have you attached to the player object?
     
  5. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    i have a rigidbody, capsule collider, scripts to control the player and an animatior
     
  6. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    If you want to access to the variable isGrounded, assuming it's inside PlayerControl script, you can do something like this:
    bool isGrounded = GetComponent<PlayerControl>().isGrounded
     
  7. Gabimela

    Gabimela

    Joined:
    Oct 3, 2019
    Posts:
    23
    now i get this : Assets\fallplayer.cs(10,23): error CS0236: A field initializer cannot reference the non-static field, method, or property 'Component.GetComponent<PlayerControl>()'
     
  8. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    I think you are missing how Unity works. Do you have a script "PlayerControl" attached to the player object and is there a public variable called isGrounded? Is there attached the script "FallingPlayer" to the player object?
     
  9. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    You have to do the GetComponent<PlayerControl>() bit inside a function, it can't be up where the bool is declared on the class. So either move the whole line inside Update(), or only declare your bool up at the top, and set its value with GetComponent inside Update()