Hey all!
I’ve hit a bit of a snag that I can’t work out and some advice would be amazing. I have a script named Interactable which inherits from MonoBehavior and from the Interactable script I have subclass scripts which inherit from it.
I’ve just written a script for cutting down trees in the game called FellableTree which inherits from Interactable, I’ve attached the FellableTree script to the trees in my scene. For whatever reason the code within the Interactable script is being run but nothing within the FellableTree script is happening.
As the only script attached the tree prefabs is FellableTree it must be functioning to a degree as it’s inheriting from Interactable just fine. Here are the scripts.
using UnityEngine;
public class Interactable : MonoBehaviour
{
public float radius = 2.7f;
bool isFocus = false;
bool hasInteracted = false;
Transform player;
public Transform interactableTransform;
public virtual void Interact()
{
}
public void OnFocus(Transform playerTransform)
{
isFocus = true;
hasInteracted = false;
player = playerTransform;
}
public void OnDefocus()
{
hasInteracted = false;
isFocus = false;
}
void Update()
{
if (isFocus == true && hasInteracted == false)
{
float distance = Vector3.Distance(player.position, interactableTransform.position);
if (distance <= radius )
{
Interact();
hasInteracted = true;
}
}
}
private void OnDrawGizmosSelected()
{
if (interactableTransform == null)
interactableTransform = transform;
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(interactableTransform.position, radius);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FellableTreeScript : Interactable
{
public int axeStrength = 2;
public Tree tree;
public override void Interact()
{
Debug.Log("Chopping down a ");
base.Interact();
FellTree();
}
void FellTree()
{
axeStrength++;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
If you have any idea why this is happening I’d really appreciate it. Thanks for taking the Time to read this, you’re awesome