0

I made a simple Die() command to deactivate my 2D Sprite, but I got an error. Any approach I can do to fix this?

    private void Die()
{
    GameObject.SetActive(false);
}

Error: CS0120 An object reference is required for the non-static field, method, or property 'GameObject.SetActive(bool)'

Kuro
  • 23
  • 3
  • In `GameObject` you need to set `SetActive` as `static` – Ibrennan208 Jul 03 '22 at 07:05
  • Or create an instance of your `GameObject` (i.e.: `var gameObject = new GameObject();`) – Ibrennan208 Jul 03 '22 at 07:05
  • Does this answer your question? [C# error: "An object reference is required for the non-static field, method, or property"](https://stackoverflow.com/questions/10264308/c-sharp-error-an-object-reference-is-required-for-the-non-static-field-method) – Ibrennan208 Jul 03 '22 at 07:06
  • @lbrennan208 not in unity. No. GameObject is a static class the problem is it doesn’t reference any instance and activation requires an instance and you dont make new ones with new for GameObjects – BugFinder Jul 03 '22 at 09:36

1 Answers1

0

The error is in the GameObject part of your code. The method you're using is not static. What you're likely looking to do is to deactivate the current GameObject to which your Component is attached. Change it to

private void Die()
{
    gameObject.SetActive(false);
}

The gameObject is a property referencing exactly that GameObject. See the documentation: https://docs.unity3d.com/ScriptReference/Component-gameObject.html

Bart
  • 19,692
  • 7
  • 68
  • 77