- Home /
missing (transform) vs none: how to code this or query for it?
Hi,
In the inspector, the drop down field used to reference transform or game object knows the difference between "none" and "missing",
that is if I delete a reference that was hooked to a public var in a script, the inspector fields shows "missing (transform)". This is very good, but I need to catch this now within my code to act upon this. I have scripted something that works, but it's really, really hacky and involves scanning all transforms... not very nice, hopefully there is a better way.
I wonder how this is coded and if the api ( editor or general) expose a function or flag for this, I failed to find anything on that matter in the help.
Thanks for your help,
Bye,
Jean
Answer by TheDemiurge · Nov 23, 2010 at 10:27 PM
Probably a memory thing. A reference marked as "null" would cause a none, while a reference to an object that's either marked for garbage-collection, or been garbage-collected, or something to that effect, would cause a missing.
The question is, do you really care about the difference or do you just want to know when your object has, for example, lots its target, and take action? If it's the former then I'm afraid I'm of no help. If it's the latter, then there are at least two ways to handle it. The easy way is to check either periodically, or on access, whether the object is there or not. The smart way is to code events using C# that your script can subscribe to, such as, say "OnLostTarget", that you can then take action on (in this case, trying to reacquire a target). The MSDN has info on how to set up a basic event system. This could also be accomplished in a simplified form using Unity's SendMessage function, but that's got a few disadvantages compared to events.
I do care about the difference indeed, I will look at that onlosttarget thing, and see how it goes. thanks.
Your answer
Follow this Question
Related Questions
How to reference a script asset 3 Answers
Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 1 Answer
How can I make one variable affect others in custom inspector? 2 Answers
Edit Static Class Config variables from editor 1 Answer
How Can I make Inspector Header, Container or GroupBox to hide and show at Unity Editor Inspector? 0 Answers