I am currently testing VR interactive events with the VR examples script, and I managed to make a loading reticle when gazing at an object for an x amount of time. However, when I put the same script on two different objects, the loading reticle is only working for one object at the time when enabled. When one is disabled, and the other one enabled, it works fine for the enabled object, but I want to add more interactive objects in one scene that have a loading reticle (being enabled). This is the script, I do not get any conflict errors or warnings. The timer works fine for every enabled object.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using VRStandardAssets.Utils;
namespace VRStandardAssets.Examples
{
// This script is a simple example of how an interactive item can
// be used to change things on gameobjects by handling events.
public class InteractiveItem : MonoBehaviour
{
[SerializeField] private Material m_NormalMaterial;
[SerializeField] private Material m_OverMaterial;
[SerializeField] private Material m_ClickedMaterial;
[SerializeField] private Material m_DoubleClickedMaterial;
[SerializeField] private Material m_GazeMaterial;
[SerializeField] private VRInteractiveItem m_InteractiveItem;
[SerializeField] private Renderer m_Renderer;
public Canvas myCanvas;
public bool enableReticle = false;
public Image loadingReticle;
public float gazeTime = 2f;
public float timer = 0f;
private void Start()
{
myCanvas = this.gameObject.GetComponentInChildren<Canvas>();
myCanvas.gameObject.SetActive(false);
loadingReticle.fillAmount = timer / 2;
}
private void Awake()
{
m_Renderer.material = m_NormalMaterial;
}
private void Update()
{
if (enableReticle == true)
{
loadingReticle.fillAmount = timer / 2;
}
else
{
timer = 0f;
}
}
private void OnEnable()
{
m_InteractiveItem.OnOver += HandleOver;
m_InteractiveItem.OnOut += HandleOut;
m_InteractiveItem.OnClick += HandleClick;
m_InteractiveItem.OnDoubleClick += HandleDoubleClick;
m_InteractiveItem.OnStare += HandleStare;
}
private void OnDisable()
{
m_InteractiveItem.OnOver -= HandleOver;
m_InteractiveItem.OnOut -= HandleOut;
m_InteractiveItem.OnClick -= HandleClick;
m_InteractiveItem.OnDoubleClick -= HandleDoubleClick;
m_InteractiveItem.OnStare -= HandleStare;
}
//Handle the Over event
private void HandleOver()
{
Debug.Log("Show over state");
m_Renderer.material = m_OverMaterial;
myCanvas.gameObject.SetActive(true);
}
//Handle the Out event
private void HandleOut()
{
Debug.Log("Show out state");
m_Renderer.material = m_NormalMaterial;
timer = 0f;
loadingReticle.fillAmount = timer / 2;
myCanvas.gameObject.SetActive(false);
}
//Handle the Click event
private void HandleClick()
{
Debug.Log("Show click state");
m_Renderer.material = m_ClickedMaterial;
}
//Handle the DoubleClick event
private void HandleDoubleClick()
{
Debug.Log("Show double click");
m_Renderer.material = m_DoubleClickedMaterial;
}
// Handle the Stare event
public void HandleStare()
{
loadingReticle.fillAmount = timer / 2;
timer += Time.deltaTime;
if (timer >= gazeTime)
{
timer = 0f;
}
}
}
}