- Home /
Annoying MissingReferenceException with Interfaces and Monobehavior
So I am somehow getting a weird misisng reference exception. I have a script "Block" that doesn't inherit from Monobehavior, but it links to an "IBlockScript" interface which is also a Monobehavior.
The issue here is that when the Monobehavior side is destroyed, the Interface side still isn't null. I can try to check if the Script is null (but that always returns false even if it was destroyed)
public IBlockScript Script { get; private set;}
public GameObject gameObject
{
get
{
//SOMEHOW Script is NEVER null even when it's Monobehavior is destroyed...
//so this check is always false.
// and the code pops an MissingRefenceException when it gets to Script.gameobject
if(Script == null || ReferenceEquals(Script,null))
return null;
try
{
return Script.gameObject;
}
catch(MissingReferenceException e)
{
return null;
}
}
}
public Transform transform
{
get
{
if(gameObject==null)
return null;
return gameObject.transform;
}
}
I'm forced to using try/catch in order to return properly and I reeaaaallly don't like doing that, is there a better way of doing this? This error typically happens when I stop the player or load a new level (when the Iblockscripts are getting unloaded)
Answer by JoshuaMcKenzie · Oct 21, 2015 at 09:39 PM
so its amazing how easy to find the solution if you just ask the question in a specific way. For anyone else that ends up here this is the solution that I've found...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Combining Lists 1 Answer
How do I get gameobjects to fire a method based on an interface? 1 Answer