Script to Hide player when H key is pressed
I want to hide my player when i press the key 'H' and came up with this code, however the character just moves the objects, and doesn't hide in them, Unity gives me this error: MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TransformBindings.gen.cs:26) CameraMovement.Update () (at Assets/Scripts/CameraMovement.cs:13) If anyone could tell me how to improve my code, or what I am doing wrong it would be greatly appreciated. Thanks.
using System.Collections; using System.Collections.Generic; using UnityEngine;
//Script to hide player when by doorways and they press the key 'H'
public class HideScript : MonoBehaviour { public Transform player; public Vector3 offset;
public void OnCollisionEnter2D(Collision2D coll)
{
if ((coll.gameObject.name == "Protag0") && Input.GetKeyDown(KeyCode.H))
{
transform.position = new Vector3(player.position.x + 0, player.position.y + 0, offset.z + -10);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612103801im_/https://answers.unity.com/themes/thub/images/avi.jpg)