Search code examples
c#unity-game-engine2dgame-enginegame-development

Game dev Unity 2d game: Problem with code and gameObject


I have a question. so i want to make the player destroy when he hits a certain sprite. Its like a void in Minecraft. It keeps saying the following: error CS0103: The name 'Player"does not exist in the current context. Can someone help me? This is the code:

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

public class Void : MonoBehaviour
{   
    
    // Start is called before the first frame update
    void Start()
    {
       gameObject = Player;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void OnCollisionEnter(Collision col)
        {   
            
              if(col.gameObject.CompareTag("Obstacle")) //If thing hit is tagged "Obstacle"
              {
                      Destroy(gameObject); //Then destroy the player
              }
         }
}

}The unity screen with important things i guess. p.s. my englis isnt that good, sorry for that!

It tried many things, i hope someone can help me...


Solution

  • if the void script attach to player, then you can destroy directly.

    void OnCollisionEnter(Collision col)
            {   
               if(col.gameObject.CompareTag("Obstacle")) //If thing hit is tagged "Obstacle"
                  {
                          Destroy(this.gameObject); //Then destroy the player
                  }
             }