Search Unity

Resolved Ambiguous/Universal Object Activation

Discussion in 'Scripting' started by Ramen11, May 18, 2022.

  1. Ramen11

    Ramen11

    Joined:
    Jun 9, 2021
    Posts:
    8
    I have a player gameobject and multiple different types of button gameobjects. I want a script on the player that will call a function on the button it hits, even if the script name is different. For example, if the player hits "e" on a red button, it would call Function1 in the red button's object, and if the player hits "e" on a green button, it will call Function2 in the green button's object.
    I don't know how I would make this work. I'm so close to the answer, yet so far. I've been trying to have a single script handle if a button has been pressed and make the player do "hit.collider.gameObject.GetComponent<Interactible>().Activate();", but that doesn't let me have the button do different things than other buttons. I think I have to make the interact script call another script on the button, but that runs me into the same problem.

    If this makes no sense, tell me and I'll try to rephrase it.

    Rephrase:
    I'm trying to make a universal "interactible" script that can be put on any object and activates another script within that object that can be set from the code or the inspector.

    OR alternatively, a way for the object-specific script to detect when the universal script's function is called.
     
    Last edited: May 18, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    One solution is interfaces... using interfaces and having your MonoBehaviours express them, you might have an
    IInteractable
    interface with a
    void Activate()
    method in it.

    Then when you are raycasting at the button you ask if it has an IInteractable interface and just Activate() it.

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
  3. Ramen11

    Ramen11

    Joined:
    Jun 9, 2021
    Posts:
    8
    I don't think this will help what I'm doing. I'm trying to make a universal interactible script that can be put on any object and activates another script within that object that can be set from the code or the inspector. Frorm my understanding, interfaces only use one name/function.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    For assigning such in the inspector, you'll likely need to use a base class as assigning interfaces in the inspector isn't something you can do easily out of the box.

    Something like this is really all you need to get started at a basic level:
    Code (CSharp):
    1. public abstract class ActivatableBase : Monobehaviour
    2. {
    3.     public abstract void Activate();
    4. }
    Then you can make classes that inherit from this, and still be able to assign them to
    ActivatableBase
    object fields in the inspector.

    Your understanding of interfaces is a bit off. They can be considered as a form of contract, that guarantee that the object that implements the interface agrees to contain certain functionality.

    In the above situation I would use both an interface + base class. The former for situations where GetComponent<T> is used, and the base class for when I need to assign in the inspector. So really it would be:

    Code (CSharp):
    1. public interface IActivateable
    2. {
    3.     public void Activate();
    4. }
    5.  
    6. public abstract class ActivateableBase : Monobehaviour, IActivateable
    7. {
    8.     public abstract void Activate();
    9. }
     
    Kurt-Dekker likes this.
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    you might be looking for UnityEvents in this case, lets you select a function from a other component to invoke. https://docs.unity3d.com/ScriptReference/Events.UnityEvent.html
     
  6. Ramen11

    Ramen11

    Joined:
    Jun 9, 2021
    Posts:
    8
    I'll do more research; I think interfaces are actually what I need after all.