Search Unity

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

Discussion in 'Scripting' started by GrimCrayon, Feb 20, 2024.

  1. GrimCrayon

    GrimCrayon

    Joined:
    Dec 23, 2021
    Posts:
    6
    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?
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,748
    mighty good question we cant see from here :p

    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?
     
    Last edited: Feb 20, 2024
    Ryiah likes this.
  3. GrimCrayon

    GrimCrayon

    Joined:
    Dec 23, 2021
    Posts:
    6
    Okay, Idk if this is what you mean, but here is my script so far :D
    It may be a bit jumbled, but idk what I'm doing tbh...
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SnailScript : MonoBehaviour
    7. {
    8.     public float moveSpeed = 1f;
    9.     private Rigidbody2D myBody;
    10.     private Animator anim;
    11.  
    12.     public LayerMask playerLayer;
    13.  
    14.     public bool moveLeft;
    15.  
    16.     private bool canMove;
    17.     private bool stunned;
    18.  
    19.     public Transform left_Collision, right_Collision, top_Collision, down_Collision;
    20.     private Vector3 left_Collision_Position, right_Collision_Position, top_Collision_Position, down_Collision_Position;
    21.  
    22.     private void Awake()
    23.     {
    24.         myBody = GetComponent<Rigidbody2D>();
    25.         anim = GetComponent<Animator>();
    26.  
    27.         left_Collision_Position = left_Collision.position;
    28.         right_Collision_Position = right_Collision.position;
    29.     }
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.         moveLeft = true;
    35.         canMove = true;
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         if (canMove)
    42.         {
    43.             if (moveLeft)
    44.             {
    45.                 myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y);
    46.             }
    47.             else
    48.             {
    49.                 myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y);
    50.             }
    51.         }
    52.  
    53.         CheckCollision();
    54.  
    55.     }
    56.  
    57.     void CheckCollision()
    58.     {
    59.         RaycastHit2D leftHit = Physics2D.Raycast(left_Collision.position, Vector2.left, 0.1f, playerLayer);
    60.         RaycastHit2D rightHit = Physics2D.Raycast(right_Collision.position, Vector2.right, 0.1f, playerLayer);
    61.  
    62.         Collider2D topHit = Physics2D.OverlapCircle(top_Collision.position, 0.1f, playerLayer);
    63.  
    64.         if (topHit != null)
    65.         {
    66.             if(topHit.gameObject.tag == "Player")
    67.             {
    68.                 if (!stunned)
    69.                 {
    70.                     topHit.gameObject.GetComponent<Rigidbody2D>().velocity =
    71.                         new Vector2(topHit.gameObject.GetComponent<Rigidbody2D>().velocity.x, 7f);
    72.  
    73.                     canMove = false;
    74.                     myBody.velocity = new Vector2(0, 0);
    75.  
    76.                     anim.Play("Stunned");
    77.                     stunned = true;
    78.  
    79.                     if(gameObject.tag == "Bettle")
    80.                     {
    81.                         anim.Play("Stunned");
    82.                         StartCoroutine(Dead());
    83.                     }
    84.                 }
    85.             }
    86.         }
    87.  
    88.         if (leftHit)
    89.         {
    90.             if (leftHit.collider.gameObject.tag == "Player")
    91.             {
    92.                 if (!stunned)
    93.                 {
    94.                     // APPLY DAMAGE TO PLAYER
    95.                     print("DAMAGE LEFT");
    96.                 }
    97.                 else
    98.                 {
    99.                     if (gameObject.tag != "Beetle")
    100.                     {
    101.                         myBody.velocity = new Vector2(15, myBody.velocity.y);
    102.                     }
    103.                 }
    104.             }
    105.         }
    106.  
    107.         if (rightHit)
    108.         {
    109.             if (rightHit.collider.gameObject.tag == "Player")
    110.             {
    111.                 if (!stunned)
    112.                 {
    113.                     // APPLY DAMAGE TO PLAYER
    114.                     print("DAMAGE RIGHT");
    115.                 }
    116.                 else
    117.                 {
    118.                     if(gameObject.tag != "Beetle")
    119.                     {
    120.                         myBody.velocity = new Vector2(-15, myBody.velocity.y);
    121.                     }
    122.                 }
    123.             }
    124.         }
    125.  
    126.         if (!Physics2D.Raycast(down_Collision.position, Vector2.down, 0.1f))
    127.         {
    128.             ChangeDirection();
    129.         }
    130.     }
    131.  
    132.     void ChangeDirection()
    133.     {
    134.         moveLeft = !moveLeft;
    135.  
    136.         Vector3 tempScale = transform.localScale;
    137.  
    138.         if (moveLeft)
    139.         {
    140.             tempScale.x = Mathf.Abs(tempScale.x);
    141.  
    142.             left_Collision.position = left_Collision_Position;
    143.             right_Collision.position = right_Collision_Position;
    144.         }
    145.         else
    146.         {
    147.             tempScale.x = -Mathf.Abs(tempScale.x);
    148.  
    149.             left_Collision.position = right_Collision_Position;
    150.             right_Collision.position = left_Collision_Position;
    151.         }
    152.  
    153.         transform.localScale = tempScale;
    154.     }
    155.  
    156.     IEnumerator Dead()
    157.     {
    158.         yield return new WaitForSeconds(0.5f);
    159.         GameObject.SetActive(false);
    160.     }
    161.  
    162. }
    163. [CODE]
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,705
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    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://forum.unity.com/threads/cant-add-script-component.632746/#post-4239121

    https://forum.unity.com/threads/update-function-not-working.953477/#post-6215873
     
  5. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,748
    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..
     
    Kurt-Dekker likes this.
  6. GrimCrayon

    GrimCrayon

    Joined:
    Dec 23, 2021
    Posts:
    6
    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 S*** about coding except the little that I have already used...
     
  7. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,748
    Well you have kinda used it already, so technically you do know..

    hint look at line 118 for hints
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,705
    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:

     
  9. GrimCrayon

    GrimCrayon

    Joined:
    Dec 23, 2021
    Posts:
    6
    OMG!!! THANK YOU SO MUCH!!! I 100% forgot that I had that in my code! This is going to help out so much!