Home > database >  The name 'gameObject' does not exist in the current context
The name 'gameObject' does not exist in the current context

Time:12-03

I can't understand why my script is not working..I don't get why this is wrong..this is my script code below.

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

public class k : MonoBehaviour
{
void OnTriggerEnter(Collider collider)
    {
        if(collider.gameObject.name == "holms")
        {
            GameVariables.keyCount =2;
            Destroy(gameobject);
        }
    }
}

I was searching the internet to find similar issue and I find similar threads but not similar to this. It different from the other post here or in google search.

CodePudding user response:

You mis-typed Destroy(gameObject)

CodePudding user response:

can you try like this

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

public class k : MonoBehaviour
{
void OnTriggerEnter(Collider collider)
    {
        if(collider.gameObject.name == "holms")
        {
            GameVariables.keyCount =2;
            Destroy(this);
        }
    }
}

CodePudding user response:

A,

If your variable gameObject is referring to one of the properties of the parameter collider, then your code must be Destroy(collider.gameObject); as mentioned by Roman Ryzhiy

  • Related