Why isn't my 'GameObject.SetActive' not working?

I am rather new to coding in Unity2D, and was wondering why I am getting an error.

I have a coroutine that I am using to wait for 3 seconds, and then disabling the object. It should be working in theory, but I am getting a strange error with my code that reads: “An object reference is required for the non-static field, method, or property ‘GameObject.SetActive(Bool)’”

Idk why it happens, but maybe someone with more knowledge on the topic could explain the problem or what I am doing wrong?

mighty good question we cant see from here :stuck_out_tongue:

Show us some minimal code (and maybe pictures) to demonstrate the issue so we can understand - and for the love of all things furry, use code tags

ps If you actually wrote GameObject.SetActive(Bool) then its right, thats like saying Human.kiss… exactly which one?

1 Like

Okay, Idk if this is what you mean, but here is my script so far :smile:
It may be a bit jumbled, but idk what I’m doing tbh…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnailScript : MonoBehaviour
{
    public float moveSpeed = 1f;
    private Rigidbody2D myBody;
    private Animator anim;

    public LayerMask playerLayer;

    public bool moveLeft;

    private bool canMove;
    private bool stunned;

    public Transform left_Collision, right_Collision, top_Collision, down_Collision;
    private Vector3 left_Collision_Position, right_Collision_Position, top_Collision_Position, down_Collision_Position;

    private void Awake()
    {
        myBody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();

        left_Collision_Position = left_Collision.position;
        right_Collision_Position = right_Collision.position;
    }

    // Start is called before the first frame update
    void Start()
    {
        moveLeft = true;
        canMove = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (canMove)
        {
            if (moveLeft)
            {
                myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
            }
            else
            {
                myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
            }
        }

        CheckCollision();

    }

    void CheckCollision()
    {
        RaycastHit2D leftHit = Physics2D.Raycast(left_Collision.position, Vector2.left, 0.1f, playerLayer);
        RaycastHit2D rightHit = Physics2D.Raycast(right_Collision.position, Vector2.right, 0.1f, playerLayer);

        Collider2D topHit = Physics2D.OverlapCircle(top_Collision.position, 0.1f, playerLayer);

        if (topHit != null)
        {
            if(topHit.gameObject.tag == "Player")
            {
                if (!stunned)
                {
                    topHit.gameObject.GetComponent<Rigidbody2D>().velocity =
                        new Vector2(topHit.gameObject.GetComponent<Rigidbody2D>().velocity.x, 7f);

                    canMove = false;
                    myBody.velocity = new Vector2(0, 0);

                    anim.Play("Stunned");
                    stunned = true;

                    if(gameObject.tag == "Bettle")
                    {
                        anim.Play("Stunned");
                        StartCoroutine(Dead());
                    }
                }
            }
        }

        if (leftHit)
        {
            if (leftHit.collider.gameObject.tag == "Player")
            {
                if (!stunned)
                {
                    // APPLY DAMAGE TO PLAYER
                    print("DAMAGE LEFT");
                }
                else
                {
                    if (gameObject.tag != "Beetle")
                    {
                        myBody.velocity = new Vector2(15, myBody.velocity.y);
                    }
                }
            }
        }

        if (rightHit)
        {
            if (rightHit.collider.gameObject.tag == "Player")
            {
                if (!stunned)
                {
                    // APPLY DAMAGE TO PLAYER
                    print("DAMAGE RIGHT");
                }
                else
                {
                    if(gameObject.tag != "Beetle")
                    {
                        myBody.velocity = new Vector2(-15, myBody.velocity.y);
                    }
                }
            }
        }

        if (!Physics2D.Raycast(down_Collision.position, Vector2.down, 0.1f))
        {
            ChangeDirection();
        }
    }

    void ChangeDirection()
    {
        moveLeft = !moveLeft;

        Vector3 tempScale = transform.localScale;

        if (moveLeft)
        {
            tempScale.x = Mathf.Abs(tempScale.x);

            left_Collision.position = left_Collision_Position;
            right_Collision.position = right_Collision_Position;
        }
        else
        {
            tempScale.x = -Mathf.Abs(tempScale.x);

            left_Collision.position = right_Collision_Position;
            right_Collision.position = left_Collision_Position;
        }

        transform.localScale = tempScale;
    }

    IEnumerator Dead()
    {
        yield return new WaitForSeconds(0.5f);
        GameObject.SetActive(false);
    }

}
[CODE]

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

ALSO: this line probably does not compile, so you’re not running this code:

GameObject is a type. You cannot set a type active.

gameObject is a shortcut property to the GameObject this script lives on.

Scripts on inactive GameObjects would never be directly executed by Unity.

Make sure your log console selector buttons are enabled. See this graphic:

https://discussions.unity.com/t/733002/10

https://discussions.unity.com/t/804947/4

So. As explained GameObject.SetActive(false); is just like saying Human.kiss, you havent said which one so it says
“An object reference is required” aka. Pick one of them dude…

1 Like

Okay… that makes sense and all, but what are the references that I supposed to use?
Srry about this… It might just be stupid, but Idk shit about coding except the little that I have already used…

Well you have kinda used it already, so technically you do know…

hint look at line 118 for hints

Sounds like you’re just trying to go too fast.

Slow down.

You have one brain. (I’m assuming here…)

That means you can work on one thing at a time.

Try this approach:

Imphenzia: How Did I Learn To Make Games:

OMG!!! THANK YOU SO MUCH!!! I 100% forgot that I had that in my code! This is going to help out so much!