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. Dismiss Notice

Assets\enemy.cs(9,9): error CS0120: An object reference is required for the non-static field, method

Discussion in 'Scripting' started by pinkpenguindev, Sep 4, 2020.

?

do you like my pic

  1. yes

    4 vote(s)
    100.0%
  2. no

    0 vote(s)
    0.0%
  1. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class enemy : MonoBehaviour
    6. {
    7.    private void  OnCollisionEnter2D(Collision2D collition)
    8. {
    9.     if (Collision.collider.GetComponent<bird>() != null)
    10.     {
    11.        Destroy(gameObject);
    12.     }
    13. }
    14.  
    15.  
    16.  
    17. }
    18.  
    fix
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Should be using "collition" (your parameter on this function) instead of "Collision" - you want the collider of this particular collision event (which is what "non-static" means in this context). "Collision" (the class) would be if you need something relating to the concept of a Collision (which would be "static").
     
  3. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
    like this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class enemy : MonoBehaviour
    6. {
    7.    private void  OnCollisionEnter2D(Collision2D collition)
    8. {
    9.     if (particular collision.collider.GetComponent<bird>() != null);
    10.     {
    11.        Destroy(gameObject);
    12.     }
    13. }
    14.  
    15.  
    16.  
    17. }
    18.  
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Nowhere did I tell you to add the word "particular" into your code?

    What you need in that place is "collition" - the name of the parameter you used on line 7.
     
  5. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
    OnGUIDepth changed: was 0 is 1. Event type was 0 help
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class enemy : MonoBehaviour
    6. {
    7.    private void  OnCollisionEnter2D(Collision2D collition)
    8. {
    9.     if ( collition.collider.GetComponent<bird>() != null);
    10.     {
    11.        Destroy(gameObject);
    12.     }
    13. }
    14.  
    15.  
    16.  
    17. }
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    That sounds like a completely separate issue?

    I just noticed: like 9 should not have a semicolon at the end.
     
  7. pinkpenguindev

    pinkpenguindev

    Joined:
    Aug 13, 2020
    Posts:
    43
    its still there
     
  8. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You've been posting a lot of threads in a short amount of time that are all related to basic syntax or logic errors, and quite frankly, come off as spammy & low-effort.

    Don't just post a script and say "fix"; describe your problem and what you've already tried to fix it.
    If you haven't tried fixing any of your problems yourself, then how do you expect to learn how to do so if you just keep passing them on the forums for someone else to figure out?

    Please take the time to do some beginner scripting tutorials; you'll save yourself a lot of time in the long run:
    https://learn.unity.com/project/beginner-gameplay-scripting
     
    bobisgod234, Eltanin9 and PraetorBlue like this.