Here is the code. When I press a key, the cube should appear, and when I let go, it disappears, but nothing comes out.
I checked using debug.log and the part where the left or right mouse button is pressed and the action is performed - DOES NOT WORK. Why? Explain, please.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class KillHit : MonoBehaviour { public GameObject hitb; // Use this for initialization void Start () { hitb = GameObject.Find("killcube"); } // Update is called once per frame void Update () { if(Input.GetMouseButton(0) || Input.GetMouseButton(1)){ hitb.gameObject.SetActive(true); }else{ hitb.gameObject.SetActive(false); Debug.Log("Works1"); } } void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.tag == "enemy01") { Destroy(other.gameObject); } } } 

    2 answers 2

      - Input.GetMouseButton - когда мышка нажата и удерживается в состоянии Down - Input.GetMouseButtonDown - когда клавиша опущена вниз - Input.GetMouseButtonUp - когда клавиша поднимается верх void Update () { if(Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)){ hitb.SetActive(true); } if(Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)){ hitb.SetActive(false); } } ps public GameObject hitb; Это и так GameObject, зачем еще раз вызывать .gameObject (hitb.gameObject.SetActive). 

      Most likely at the time of launching the scene your "killcube" already disabled. Unity cannot receive GameObject and its components if that is disconnected SetActive(false) '

      To make it work, you must get a link to your GameObject hitb . Simply put, in the beginning get another "killcube" , and then disable it using the SetActive(false) method.

      And, and using the GameObject.Find(string name) command is not beneficial from the side of performance, it was easier to transfer your object directly to UnityEditor.