Hello everyone! I am just getting started with development for touch interfaces and I decided to make a base class to contain all the functions I need and then inherit those functions whenever I want to use them. This is the script of the base class
using UnityEngine;
using System.Collections;
public abstract class TouchScreenBaseClass : MonoBehaviour {
public void ReadTouches(){
foreach(Touch t in Input.touches){
Ray ray = Camera.main.ScreenPointToRay(t.position);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
if(t.phase.Equals(TouchPhase.Began)){
OnTouchDown();
}
if(t.phase.Equals(TouchPhase.Ended) || t.phase.Equals(TouchPhase.Canceled)){
OnTouchUp();
}
if(t.phase.Equals(TouchPhase.Stationary)|| t.phase.Equals(TouchPhase.Moved)){
OnTouchStay();
}
}
}
}
public virtual void OnTouchDown(){
}
public virtual void OnTouchUp(){
}
public virtual void OnTouchStay(){
}
}
After creating this class I created an other class which basically just changed the material of an object.
using UnityEngine;
using System.Collections;
public class Colors : TouchScreenBaseClass {
public Material m1;
public Material m2;
public Material m3;
void Update(){
ReadTouches ();
}
override public void OnTouchDown(){
gameObject.renderer.material = m1;
}
override public void OnTouchStay(){
gameObject.renderer.material = m2;
}
override public void OnTouchUp(){
gameObject.renderer.material = m3;
}
}
The problem is that if I have multiple cubes with this script when I touch any of the cubes they all change color as if some function in the script were static or something like that.
Any help on this matter or anything related to touchscreen inputs would be greatly appreciated, so thank you in advance.