Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Error CS0120

Discussion in 'Editor & General Support' started by LordEnma, Aug 30, 2020.

  1. LordEnma

    LordEnma

    Joined:
    Aug 16, 2020
    Posts:
    16
    My problem seems really simple, but every solution I find online doesn't work. I get the Error CS0120

    Error CS0120: An object reference is required for the non-static field, method, or property 'bl_Joystick.Horizontal'
    My C# code is as follows:

    {
    public Rigidbody rb;

    public float forwordForce = 1000f;
    public float sidewayForce = 500f;

    void FixedUpdate()
    {
    rb.AddForce(0, 0, forwordForce * Time.deltaTime);

    if (Input.GetKey("d"))
    {
    rb.AddForce(sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if (Input.GetKey("a"))
    {
    rb.AddForce(-sidewayForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }

    if (rb.position.y < -15f)
    {
    FindObjectOfType<GameManager>().GameOver();
    }

    bool isKeyPressed = (bl_Joystick.Horizontal > 0) ? true : false;

    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Ghe error is straight forward. You need a reference to an actual instance of bl_Joystick to call functions on it. I'm not sure how that class is set up since you didn't share it, so how you get that instance depends on how it's set up (does it use a Singleton pattern?)
     
  3. LordEnma

    LordEnma

    Joined:
    Aug 16, 2020
    Posts:
    16
    here's the other code which might help you

    Code (CSharp):
    1. public class bl_Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    2. {
    3.     public bl_Joystick Joystick;
    4.  
    5.  
    6.     [Header("Settings")]
    7.     [SerializeField, Range(1, 15)]private float Radio = 5;//the ratio of the circumference of the joystick
    8.     [SerializeField, Range(0.01f, 1)]private float SmoothTime = 0.5f;//return to default position speed
    9.     [SerializeField, Range(0.5f, 4)] private float OnPressScale = 1.5f;//return to default position speed
    10.     public Color NormalColor = new Color(1, 1, 1, 1);
    11.     public Color PressColor = new Color(1, 1, 1, 1);
    12.     [SerializeField, Range(0.1f, 5)]private float Duration = 1;
    13.  
    14.     [Header("Reference")]
    15.     [SerializeField]private RectTransform StickRect;//The middle joystick UI
    16.     [SerializeField] private RectTransform CenterReference;
    17.  
    18.     //Privates
    19.     private Vector3 DeathArea;
    20.     private Vector3 currentVelocity;
    21.     private bool isFree = false;
    22.     private int lastId = -2;
    23.     private Image stickImage;
    24.     private Image backImage;
    25.     private Canvas m_Canvas;
    26.     private float diff;
    27.     private Vector3 PressScaleVector;
    28.  
    29.     /// <summary>
    30.     ///
    31.     /// </summary>
    32.     void Start()
    33.     {
    34.         if (StickRect == null)
    35.         {
    36.             Debug.LogError("Please add the stick for joystick work!.");
    37.             this.enabled = false;
    38.             return;
    39.         }
    40.  
    41.         if (transform.root.GetComponent<Canvas>() != null)
    42.         {
    43.             m_Canvas = transform.root.GetComponent<Canvas>();
    44.         }
    45.         else if (transform.root.GetComponentInChildren<Canvas>() != null)
    46.         {
    47.             m_Canvas = transform.root.GetComponentInChildren<Canvas>();
    48.         }
    49.         else
    50.         {
    51.             Debug.LogError("Required at lest one canvas for joystick work.!");
    52.             this.enabled = false;
    53.             return;
    54.         }
    55.      
    56.         //Get the default area of joystick
    57.         DeathArea = CenterReference.position;
    58.         diff = CenterReference.position.magnitude;
    59.         PressScaleVector = new Vector3(OnPressScale, OnPressScale, OnPressScale);
    60.         if (GetComponent<Image>() != null)
    61.         {
    62.             backImage = GetComponent<Image>();
    63.             stickImage = StickRect.GetComponent<Image>();
    64.             backImage.CrossFadeColor(NormalColor, 0.1f, true, true);
    65.             stickImage.CrossFadeColor(NormalColor, 0.1f, true, true);
    66.         }
    67.     }
    68.  
    69.     /// <summary>
    70.     ///
    71.     /// </summary>
    72.     void Update()
    73.     {
    74.         DeathArea = CenterReference.position;
    75.         //If this not free (not touched) then not need continue
    76.         if (!isFree)
    77.             return;
    78.  
    79.         //Return to default position with a smooth movement
    80.         StickRect.position = Vector3.SmoothDamp(StickRect.position, DeathArea, ref currentVelocity, smoothTime);
    81.         //When is in default position, we not need continue update this
    82.         if (Vector3.Distance(StickRect.position, DeathArea) < .1f)
    83.         {
    84.             isFree = false;
    85.             StickRect.position = DeathArea;
    86.         }
    87.     }
    88.  
    89.     /// <summary>
    90.     /// When click here event
    91.     /// </summary>
    92.     /// <param name="data"></param>
    93.     public void OnPointerDown(PointerEventData data)
    94.     {
    95.         //Detect if is the default touchID
    96.         if (lastId == -2)
    97.         {
    98.             //then get the current id of the current touch.
    99.             //this for avoid that other touch can take effect in the drag position event.
    100.             //we only need get the position of this touch
    101.             lastId = data.pointerId;
    102.             StopAllCoroutines();
    103.             StartCoroutine(ScaleJoysctick(true));
    104.             OnDrag(data);
    105.             if (backImage != null)
    106.             {
    107.                 backImage.CrossFadeColor(PressColor, Duration, true, true);
    108.                 stickImage.CrossFadeColor(PressColor, Duration, true, true);
    109.             }
    110.         }
    111.     }
    112.  
    113.     /// <summary>
    114.     ///
    115.     /// </summary>
    116.     /// <param name="data"></param>
    117.     public void OnDrag(PointerEventData data)
    118.     {
    119.         //If this touch id is the first touch in the event
    120.         if (data.pointerId == lastId)
    121.         {
    122.             isFree = false;
    123.             //Get Position of current touch
    124.             Vector3 position = bl_JoystickUtils.TouchPosition(m_Canvas,GetTouchID);
    125.  
    126.             //Rotate into the area circumferential of joystick
    127.             if (Vector2.Distance(DeathArea, position) < radio)
    128.             {
    129.                 StickRect.position = position;
    130.             }
    131.             else
    132.             {
    133.                 StickRect.position = DeathArea + (position - DeathArea).normalized * radio;
    134.             }
    135.         }
    136.     }
    137.  
    138.     /// <summary>
    139.     /// When touch is Up
    140.     /// </summary>
    141.     /// <param name="data"></param>
    142.     public void OnPointerUp(PointerEventData data)
    143.     {
    144.         isFree = true;
    145.         currentVelocity = Vector3.zero;
    146.         //leave the default id again
    147.         if (data.pointerId == lastId)
    148.         {
    149.             //-2 due -1 is the first touch id
    150.             lastId = -2;
    151.             StopAllCoroutines();
    152.             StartCoroutine(ScaleJoysctick(false));
    153.             if (backImage != null)
    154.             {
    155.                 backImage.CrossFadeColor(NormalColor, Duration, true, true);
    156.                 stickImage.CrossFadeColor(NormalColor, Duration, true, true);
    157.             }
    158.         }
    159.     }
    160.  
    161.     /// <summary>
    162.     ///
    163.     /// </summary>
    164.     /// <returns></returns>
    165.     IEnumerator ScaleJoysctick(bool increase)
    166.     {
    167.         float _time = 0;
    168.  
    169.             while (_time < Duration)
    170.             {
    171.                 Vector3 v = StickRect.localScale;
    172.             if (increase)
    173.             {
    174.                 v = Vector3.Lerp(StickRect.localScale, PressScaleVector, (_time / Duration));
    175.             }
    176.             else
    177.             {
    178.                 v = Vector3.Lerp(StickRect.localScale, Vector3.one, (_time / Duration));
    179.             }
    180.             StickRect.localScale = v;
    181.                 _time += Time.deltaTime;
    182.                 yield return null;
    183.             }
    184.     }
    185.    
    186.  
    187.     /// <summary>
    188.     /// Get the touch by the store touchID
    189.     /// </summary>
    190.     public int GetTouchID
    191.     {
    192.         get
    193.         {
    194.             //find in all touches
    195.             for (int i = 0; i < Input.touches.Length; i++)
    196.             {
    197.                 if (Input.touches[i].fingerId == lastId)
    198.                 {
    199.                     return i;
    200.                 }
    201.             }
    202.             return -1;
    203.         }
    204.     }
    205.  
    206.     private float radio { get { return (Radio * 5 + Mathf.Abs((diff - CenterReference.position.magnitude))); } }
    207.     private float smoothTime { get { return (1 - (SmoothTime)); } }
    208.  
    209.     /// <summary>
    210.     /// Value Horizontal of the Joystick
    211.     /// Get this for get the horizontal value of joystick
    212.     /// </summary>
    213.     public float Horizontal
    214.     {
    215.         get
    216.         {
    217.             return (StickRect.position.x - DeathArea.x) / Radio;
    218.         }
    219.     }
    220.  
    221.     /// <summary>
    222.     /// Value Vertical of the Joystick
    223.     /// Get this for get the vertical value of joystick
    224.     /// </summary>
    225.     public float Vertical
    226.     {
    227.         get
    228.         {
    229.             return (StickRect.position.y - DeathArea.y) / Radio;
    230.         }
    231.     }
    232. }
     
  4. LordEnma

    LordEnma

    Joined:
    Aug 16, 2020
    Posts:
    16
    if this doesn't help, then I'll send the other scripts since I bought this off the unity asset store.
     
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You need to access an instance of the class.

    Try adding a public bl_Joystick to your original script, dragging and dropping the bl_Joystick onto that field in the inspector, and using that variable when assigning to "bool isKeyPressed".
     
    PraetorBlue likes this.