Search Unity

'GameObject' is a type, which is not valid in the given context

Discussion in 'Scripting' started by Ivoknoob, Jun 12, 2022.

  1. Ivoknoob

    Ivoknoob

    Joined:
    Jun 12, 2022
    Posts:
    3
    I have this Error when i try to make PIckup script. Can someone Help me to figure it out.
    The Error is 'GameObject' is a type, which is not valid in the given context.
    And the script is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup : MonoBehaviour
    6. {
    7.     private Inventory inventory;
    8.     public GameObject itemButton;
    9.  
    10.     private void Start()
    11.     {
    12.         inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
    13.     }
    14.  
    15.     void OnTriggerEnter2D(Collider2D other) {
    16.  
    17.         if (other.CompareTag("Player")) {
    18.             for (int i = 0; i < inventory.slots.Length; i++)
    19.             {
    20.                 if (inventory.isFull[i] == false) {
    21.                     inventory.isFull[i] = true;
    22.                     Instantiate(itemButton, inventory.slots[i].transform, false);
    23.                     Destroy(GameObject);
    24.                     break;
    25.                 }
    26.             }
    27.         }
    28.     }
    29. }
    30.  
     
  2. Ivoknoob

    Ivoknoob

    Joined:
    Jun 12, 2022
    Posts:
    3
    the error is on line 23
     
  3. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Change:
    Code (CSharp):
    1. Destroy(GameObject);
    to:
    Code (CSharp):
    1. Destroy(gameObject);
     
  4. Ivoknoob

    Ivoknoob

    Joined:
    Jun 12, 2022
    Posts:
    3
    Thanks :D
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
     
  6. Xender1

    Xender1

    Joined:
    Jul 9, 2022
    Posts:
    1
    try this Destroy(gameObject);