Hey guys,
I’m working on UI and HUD inputs, and I came into a little road block when working on my swiping events, let me explain:
I have a main GUI_Controls class which turns things on/off depending on what input is coming in. In that class, I have a reference to my Swipe class, which I use on multiple HUD elements which need to detect swiping events:
using UnityEngine;
using System;
public class GUI_Controls : MonoBehaviour {
public Swipe swipe;
void Awake(){
if( !swipe ) swipe = FindObjectOfType(typeof(Swipe)) as Swipe;
}
void Update(){
if( swipe.SwipeDistance > 200 ) Debug.Log("SWIPE SUCCESSFUL!");
}
}
The main problem with the code above is that ‘swipe’ class reference only references 1 of the HUD elements which has it attached. So the only ‘SwipeDistance’ it’s actually checking against is the one from the referenced gameObject, which makes all the other swipes on other HUD elements no work correctly.
Is there a way to assign the ‘swipe’ reference from whichever gameObject is being swiped? For example, in my ‘Swipe’ class, can I do something like this?:
using UnityEngine;
using System;
public class Swipe : MonoBehaviour {
public GUI_Controls controls;
void Update() {
controls.swipe = this;
}
}
I can’t try this right now, or I would instead of asking. But will this even work? Should I be doing something different, like maybe sending messages instead of directly accessing variables etc…? Also, I wanted to make the ‘Swipe’ class independent of any other class, so assigning GUI_Controls’s swipe variable from within ‘Swipe’ kinda breaks that independence…
I am mostly used to using Singletons, but when I realized a lot of my classes shouldn’t be Singletons, I started to convert them so that they could be used at multiple places at the same time, but in some occasions like the one above, I am not sure which technique to use, or what are the best practices for those scenarios…
Thanks for your time guys, and hopefully someone can shade some light on that little problem for me
Stephane