Grab an inherited subclass script from a GameObject in C#
Hey all,
New to Unity. Trying to make an interaction script that lets actions occur on RaycastHit with a GameObject tagged as "Interactable". The idea is to make a behavior script shell that works thusly:
using UnityEngine;
using System.Collections;
public class InteractableBehavior : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void InteractWith(Camera interactor) {
}
public void StopInteraction() {
}
}
And is then extended to specify specific behavior:
using UnityEngine;
using System.Collections;
public class BlockBehavior : InteractableBehavior {
Camera holder;
bool interactionActive = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(interactionActive) {
transform.position = holder.transform.position+holder.transform.forward;
}
}
public new void InteractWith(Camera interactor) {
holder = interactor;
interactionActive = true;
}
public new void StopInteraction() {
interactionActive = false;
}
}
This way, I can just grab the GameObject from the RaycastHit:
interactionTarget = hit.transform.gameObject;
do some step
????
and call interactionTarget.interactWith(playerCamera) or some equivalent. I'm just not used to C# and grabbing scripts from GameObjects. Essentially I want to specify that I'll be using a script of type "InteractableBehavior" instead of "BoxBehavior", "DialogueBehavior", etc so that the interface can be uniform. Right now I'm trying:
InteractableBehavior interactionScript = interactionTarget.GetComponent<InteractableBehavior>();
but it's not working. I'm open to suggestions.
Hope this makes sense. Thanks in advance for the help!