question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RequiredComponent with interfaces can create a nullreference exception

See original GitHub issue

Summary

Adding a component to a gameobject with a RequiredComponent attribute that has a interface as type will result in a exception if that gameobject does not have a component with that interface. It seems to attempt to make a instance of that interface which is not possible since its a interface.

A better more clearer error message is needed than a nullreference exception. It should be clear to the user that you cannot add this component to a gameobject if that gameobject does not have a component that implements the required interface.

How to reproduce [Bugs]

  • Add the following code:
public interface ITest { }

[RequiredComponent(typeof(ITest))]
public class Test : Component { }

-Try adding the Test component to a gameobject that does not have a component that implements ITest. -Profit

Workaround [Bugs]

  • Just ignore the error

Attachments

The error: [Edit] Error: NullReferenceException: Object reference not set to an instance of an object. CallStack: at System.Object.GetType() at Duality.Editor.UndoRedoActions.CreateComponentAction.SetupComponentForEditing(Component cmp) in c:\projects\duality\Source\Editor\DualityEditor\UndoRedoActions\CreateComponentAction.cs:line 108 at Duality.Editor.UndoRedoActions.CreateComponentAction.Do() in c:\projects\duality\Source\Editor\DualityEditor\UndoRedoActions\CreateComponentAction.cs:line 72 at Duality.Editor.Plugins.SceneView.SceneView.CreateComponent(TreeNodeAdv baseNode, Type cmpType) in c:\projects\duality\Source\Plugins\EditorModules\SceneView\Modules\SceneView.cs:line 514 at Duality.Editor.Plugins.SceneView.SceneView.newToolStripMenuItem_ItemClicked(Object sender, EventArgs e) in c:\projects\duality\Source\Plugins\EditorModules\SceneView\Modules\SceneView.cs:line 1531 at AdamsLair.WinForms.ItemModels.MenuModelItem.RaisePerformAction() in c:\Stuff\svn\Projects\AdamsLair.WinForms\WinForms\ItemModels\MenuModelItem.cs:line 318 at AdamsLair.WinForms.ItemViews.MenuStripMenuView.viewMenuItem_Click(Object sender, EventArgs e) in c:\Stuff\svn\Projects\AdamsLair.WinForms\WinForms\ItemViews\MenuStripMenuView.cs:line 306 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
deanljohnsoncommented, May 18, 2018

Okay, I think I have something that works pretty well. First, here is the class setup:

public class D : Component, IRequirementInterface {}

[RequiredComponent(typeof(IRequirementInterface))]
public class C : Component , IRequirementInterface {}

[RequiredComponent(typeof(C))]
public class B : Component, IRequirementInterface {}

[RequiredComponent(typeof(IRequirementInterface))]
public class A : Component {}

And the results:

required-interface

In the failure case there is no exception thrown and no undo/redo action is available.

0reactions
ilexpcommented, Jun 3, 2018

Done by @deanljohnson. Closing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unhelpful error messages, when using RequireComponent ...
But when a GameObject has no Monobehaviour attached - which implements this interface - Unity error message only says: `NullReferenceException: ...
Read more >
c# - What is a NullReferenceException, and how do I fix it?
This means the reference is null , and you cannot access members (such as methods) through a null reference. The simplest case: string...
Read more >
What is a Null Reference Exception?
NullReferenceException happens when your script code tries to use a variable which isn't set (referencing) and object. The error message that appears tells...
Read more >
debugging - NullReferenceException in Unity
NullReferenceException are thrown when you try to access a reference variable that isn't referencing any object, hence it is null (memory ...
Read more >
Interfaces in C#: Null Reference Exception. : r/csharp
Hey everyone. I've got an app I am working on in ASP Boiler Plate and I cannot remember the correct way to reference...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found