Finding unique interacting function within gameObject
In a 2D game, I have a script with two functions: 1) Scans for possible things to interact with 2) Interacts with any possible scanned object by pulling out an interacting function within the object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerScanner : MonoBehaviour {
public KeyCode buttonCheck = KeyCode.X;
public LayerMask movingLayer;
private BoxCollider2D boxCollider;
private Animator anim;
void Start () {
anim = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
void Update () {
//Later for scan function
}
//Scans if space is filled by interactable object
void Scan(int xDir, int yDir, out RaycastHit2D hit)
{
Vector2 start = transform.position;
Vector2 end = start + new Vector2(xDir, yDir);
boxCollider.enabled = false;
hit = Physics2D.Linecast(start, end, movingLayer);
boxCollider.enabled = true;
if(hit.transform != false)
{
Interact(hit.transform.gameObject);
}
}
//Interacts with object at that space
void Interact(GameObject target)
{
}
}
This is what I have so far, and I have a function in a chest game object called "Activate()". Is there any way to trigger this function from my PlayerScanner script? I want this to be flexible, so I can make different types of gameObjects with different functions in the future, that can still be read and called on.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150205im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Grab an inherited subclass script from a GameObject in C# 0 Answers
Set One GameObject Inactive If Another GameObject Is Active? 1 Answer
how do i check if a gameobject is not active? 0 Answers
How to store all components of a GameObject in an array? 0 Answers
Unity C# how to Foreach variable in Model/Contract 0 Answers