greeting guys
i am doing a simple little experiment with C# interface and i think i am stuck
here is the interface:
/////////////
using UnityEngine;
using System.Collections;
public interface changeColor_interface
{
void changeColor();
void setColorBack();
}
//////////////
and here is one of the implementation:
//////////////
using UnityEngine;
using System.Collections;
public class changeGreen : MonoBehaviour,changeColor_interface
{
private MeshRenderer gRenderer;
void Awake()
{
gRenderer = GetComponent();
}
#region changeColor_interface implementation
public void changeColor ()
{
gRenderer.material.color = Color.green;
}
public void setColorBack ()
{
gRenderer.material.color = Color.red;
}
#endregion
}
/////////////////////
then here is where i wanna call it base on the interface:
////////////////////
using UnityEngine;
using System.Collections;
public class moverColliderTriggerScript : MonoBehaviour
{
public changeColor_interface[ ] Objects;
private changeColor_interface getter;
void OnTriggerStay(Collider other)
{
if (other.GetComponent(typeof(changeColor_interface)))
{
getter = other.GetComponent(typeof(changeColor_interface));
getter.changeColor();
// other.BroadcastMessage(“changeColor”,SendMessageOptions.DontRequireReceiver);
}
}
///////////////////////////////////////////
the red line is source of the problem(without the green line), it would return error saying sth “cant implicitly convert component to #$%#”,
how come i cannot assign the type into the same type variable?
i end up using the green line (without the red line) to make this work.
but come to think about it, if i was using the green line, then what is the point of using interface at all, since broadcast message can call a specific method to be executed