I wanna make a menu in my game if hold down Q and press the button for an object that the menu falls away and I get the object in hand.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryFlashLight : MonoBehaviour
{
public GameObject Item;
public GameObject UiItem;
public bool paused;
public Button yourButton;
private FirstPersonAIO myMove;
void Start()
{
myMove = GetComponent<FirstPersonAIO>();
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
if (paused)
{
Time.timeScale = 0;
}
if (!paused)
{
Time.timeScale = 1;
}
paused = !paused;
myMove.enabled = true;
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
UiItem.SetActive(false);
Item.SetActive(true);
}
}
here are the screenshots of insipector.
The on click doesn’t show on click task
It gives me this error
NullReferenceException: Object reference not set to an instance of an object
InventoryFlashLight.TaskOnClick () (at Assets/Script/InventoryFlashLight.cs:36)
UnityEngine.Events.InvokableCall.Invoke () (at <0f484ee213264ebc81764f2bdb13f359>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <0f484ee213264ebc81764f2bdb13f359>:0)
UnityEngine.UI.Button.Press () (at D:/Unity/2020.2.0a11/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at D:/Unity/2020.2.0a11/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at D:/Unity/2020.2.0a11/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at D:/Unity/2020.2.0a11/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:262)
UnityEngine.EventSystems.EventSystem:Update() (at D:/Unity/2020.2.0a11/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:384)
I don’t know what it means. or how to solve it my English isn’t very great. Please help me. Thanks in advance