Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
3 captures
14 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fireye04 · yesterday · scripting problemtriggergetcomponenttriggersinterface

GetComponent(); returning null

So I'm trying to make an interface system for interactable items. Here's the interface:

 public interface IInteractable {

     // Can be interacted with
     public bool inter { get; set; }

     // Interact function
     public void Interact();
 }

I took this and implemented it in the following test script, which I attached to a GameObject with proper colliders.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Test_NPC : MonoBehaviour, IInteractable {
 
     public bool inter { get; set; }
 
     public void Interact() {
         Debug.Log("Interact");
     }
 }

Then I took a 2D Box collider and connected it to the player and attached the following script to it:

 private void OnTriggerEnter2D(Collider2D other) { 
     var interactable = other.gameObject.GetComponent<IInteractable>();
     interactable.Interact();
 }

It didn't work, so I went in with a breakpoint, and- alas- interactable was returning null. other.gameObject was properly returning the NPC GameObject.

I've been struggling with this for the past few days, so I've been progressively simplifying the functions to no avail. Any help is greatly appreciated!

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · yesterday 1
Share

Are you sure other.gameObject has the script you're looking for? Not a parent or child?

You can use simple Debug.Logs to check the components attached

  private void OnTriggerEnter2D(Collider2D other) { 
      var components = other.gameObject.GetComponents<Component>();
      Debug.Log(other.name + " has the following components:" + string.Join("", System.Array.ConvertAll(components, c => "\n- " + c.GetType() + (c is IInteractable ? " implements" : " does not implement") + " IInteractable"));
      var interactable = other.gameObject.GetComponent<IInteractable>();
      interactable.Interact();
  }

avatar image Fireye04 Hellium · 18 hours ago 0
Share

other.gameObject has the Test_NPC script displayed. That's the second example in the above post. It has the IInteractable interface implemented, so I don't think that's the issue, unless I'm sorely mistaken.

alt text

code.png (32.1 kB)
avatar image Hellium Fireye04 · 17 hours ago 0
Share

What does the Debug.Log output?

Show more comments
avatar image Cfaz · 18 hours ago 0
Share

does it work if you replace GetComponent<IInteractable> with GetComponent<Test_NPC> ?

avatar image Fireye04 Cfaz · 18 hours ago 0
Share

Yep, it does. Now the only issue is how to implement the interface itself!

avatar image Bunny83 Fireye04 · 14 hours ago 0
Share

While I haven't used interfaces on MonoBehaviours that often, I can say I have used them (even a long time ago before the generic GetComponent version could be used) and it always worked flawlessly. So your result seems strange. What if you iterate through your Component array and try to cast all the components and see which one works?

 foreach(var comp in components)
 {
     if (comp is IInteractable inter)
     {
         Debug.Log("Found IInteractable");
         inter.Interact();
     }
 }

If this does not work, I would assume that you have multiple "IInteractable" definitions and you use one on the class and the other in your script.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Fireye04 · 12 hours ago

I don't know how or why, but renaming the interactable variable in the box collider script to interable fixed everything. Then I took the Test_NPC script and modified it a bit:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class Test_NPC : MonoBehaviour, IInteractable {
  
    private bool _inter;

    public bool inter { get { return _inter; } set { _inter = value; } }
 
     public void Interact() {
         Debug.Log("Interact");
     }

     void Awake() {
         _inter = true;
     }

 }

And the code worked perfectly. Hopefully this helps anyone stumbling across this in the future.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · 2 hours ago 0
Share

I doubt this would help anyone as your changes can not have any effect on the behaviour of GetComponent. I've seen in your screenshot where you showed the "components" array, your line of execution is on your getcomponents line. That means it has not been executed yet. So at this point "interactable" would still be null.


Maybe you originally had an issue where you did not save some of your changes in VS (for example the "IInteractable" was missing on the "Test_NPC" class?)


local variable names have no meaning to the compiler as local variable names are not remembered at all. local variables just have an offset from the current stackframe once compiled. Also scope clashes can't be the issue either as local scope always beats higher scopes.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

270 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter2D crashing editor 0 Answers

I want to use an interface but the editor doesn't show public properties. What should I do ? 3 Answers

Animator Trigger Not Working 1 Answer

,How to get the script of an element? or the GameObject of an Component? 1 Answer

How do I make a button appear when my player enters a trigger? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges