Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Getting a specific script that implements an interface from raycasting.

Discussion in 'Scripting' started by Tayd3ll, Jul 29, 2020.

  1. Tayd3ll

    Tayd3ll

    Joined:
    Jun 4, 2015
    Posts:
    13
    Hey guys,

    I have a few different objects i want to interact with but i want them to do different things and be named different things. Is there a way to just add an interface that has the method name i want to call. and from a raycast collide and call the script that has that interface on it?

    I dont want to do a check for each specific object on my player i just want to have a generic check for the script containing the interface and call the interaction method.

    Example:

    Code (CSharp):
    1. IInteractions interactable = raycastHit.collider.gameObject.GetComponent<IInteractions>();
    2. interactable.Interaction();
    something like this?
     
  2. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    Looks good to me ... so long as you want to interact with all of them in the same way. Otherwise Interaction() will have to call some other function somewhere to handle the specific logic.
     
  3. Tayd3ll

    Tayd3ll

    Joined:
    Jun 4, 2015
    Posts:
    13
    Yea i have been spending some time looking through the docs and i wasnt able to find anything that actually let me get a component that had an interface attached. apparently you can't because the interface isnt technically the component. I got around it by calling a manager that took in a game object and from there it decided on the script to use and calls the Interaction().

    Thanks for your response still!
     
    AnthonySharp likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    GetComponent works just fine with interfaces in recent unity versions. Are you seeing different?

    As for different behaviors that can be handled by the different implementations of Interaction on each script.
     
  5. Tayd3ll

    Tayd3ll

    Joined:
    Jun 4, 2015
    Posts:
    13
    In my use case I have a script specific to the object called workerInteraction which implements Iinteractions and has the method Interact on it.

    I was attempting to on raycasthit grab the script with the IInteractions interface attached to it so I could call Interact instead of having to have abunch of if statements to see what I hit or have 1 giant script on everything i could interact with. I tried using the GetComponent<IInteractions>() but wasnt having any luck. I tried it with type of aswell. But it's good to know it is possible and I just messed something ekse up.. Thanks!!
     
  6. Tayd3ll

    Tayd3ll

    Joined:
    Jun 4, 2015
    Posts:
    13
    Hey I went through and retried it and it does indeed work! i was picking apart what i was actually interacting with in my scene and i moved all my interactable objects to there own layermask. but the code i innitially posted does work!
    Thanks for letting me know it shoulc have worked so i would go back and check it out and dig deeper into why it wasnt!
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Most likely your raycast was hitting a different object than you were expecting.