Is it safe to destroy null - unity3d

I have a component that creates several GameObject's and I would like to destroy those objects when the component is destroyed. The game object is not created in Start so it would be possible that it is null when OnDestroy is called for the component. So is it safe to do Destroy(null) in Unity? or should I check for null before trying to destroy an object?

As Daniel answered, this is an easy thing to try, so try it yourself, but let's say you won't be able to tell if the component is null or notÙˆ You can use try
like that
try{
Destroy(Some.gameObject);
}
catch{}
Although it is not known whether the component is empty or not It is almost impossible

Related

How to destroy a class in C#

i was trying to destroy a class when i want, but it just doesn't get destroyed.
I want to destroy it even if it had references in other places
and and turn those references into nulls.
Like in unity when you create a component and you destroy it and it gets destroyed.
And all references turn into nulls.
if there is a way please tell me, Thanks.
I tried IDispose but i don't know if it's wrong.
A reference to it should be null but its not.
I called the dispose function but a reference to it says its not null.

Having a list with a script and getting the gameobject from it

I have a script that has a list with the type being a certain script. Then wanting to instantiate all of the members of this list (GameObjects have this script). I've tried doing a for loop over the list and then doing .GetComponent<GameObject>(); but of course that doesn't work for obvious reasons. So Is there a way to do this?
For anyone wondering why I'm trying to do this, I want to limit anyone else that is using this script to only add gameobjects with this script (since I don't want gameobject without this script being spawned)
Maybe having a reference on the certain script that knows what gameobject has been attached to it? or is there a better way to do this
Thank you :)
It would be easier if you added your code to the question.
Well, GameObject is obviously not of type Component (MonoBehaviour inherits from Component).
It sounds like what you have is a List<SomeComponent>.
So you can simply access the property Component.gameObject which is the reference to the GameObject that component is attached to.
List<SomeComponent> list;
...
foreach(var component in list)
{
var obj = component.gameObject;
...
}
Note however that you actually don't need this. It sounds like you want to pass the elements of the list to Instantiate which also takes a Component (or better said any Object the mother class of GameObject, Component and ScriptableObject) as prefab parameter. It then returns the same type of the given prefab.

MRTK Add ManipulationHandler in C#

I'm attempting to dynamically add Manipulation Events to a ManipulationHandler that is being added to child objects of a parent. The parent object will be what the user is inspecting, but the user will be able to grab parts off of the parent and inspect them more closely. (i.e. you can look at an engine (parent object), but if you want to inspect the pistons (child objects) you can grab them and look at them)
Instead of having to go into every child object and manually add it in Unity I'd like to be able to add the parent object and just procedurally add the ManipulationHandler and ManipulationEvents on start or awake.
So far I have the following code for adding the ManipulationHandler script, but to add the ManipulationEvent I'm not sure how to set up the pointers so I can use the script and function I want from the source:
gameObject.AddComponent<ManipulationHandler>();
ManipulationHandler handler = gameObject.GetComponent<ManipulationHandler>();
ManipulationEvent newevent = new ManipulationEvent();
ManipulationEventData eventdata = new ManipulationEventData();
eventdata.ManipulationSource = gameObject;
The program works when I grab the objects, but I'd like to add manipulation events when I grab them so I can display additional data.
I see there's a getter and setter for Pointer in ManipulationEventData, but I'm not sure how to instantiate IMixedRealityPointer and how to get it to work. I'm also not sure if that's the object I actually need to accomplish what I'd like to accomplish.
I apologize in advance if I've missed something obvious. I'm new to MRTK.
Thanks!
The ManipulationHandler has four callback events among them OnManipulationStarted and OnManipulationEnded you can simply add listeners to. (see UnityEvent.AddListener)
Unless I understood your question wrong you don't have to instantiate any IMixedRealityPointer. You don't create the event data yourself but rather the ManipulationHandler feeds these events with the current event data including the interacting pointer information. The ManipulationHandler uses OnPointerDown and OnPointerDragged and OnPointerUp via the IMixedRealityPointerHandler interface in order to manage the interacting pointers and invokes the according events where needed.
Instead of using AddComponent followed by GetComponent directly store and use the return value of AddComponent which will be the reference of the newly added Component. MRTK also has an extension method
T EnsureComponent<T>(this Component component) where T : Component
so that you can simply use e.g.
var handler = this.EnsureComponent<ManipulationHandler>();
which internally first checks whether the component already exists, and if not it adds it.
Note that in order to enable near interactions you will also need a NearInteractionGrabbable so you should add this one too.
You also will have to make sure that your objects have some sort of Collider attached to the same GameObject as the NearInteractionGrabbable.
...
gameObject.transform.EnsureComponnet<NearInteractionGrabbable>();
var handler = gameObject.transform.EnsureComponnet<ManipulationHandler>();
handler.OnManipulationStarted.AddListener(HandleOnManipulationStarted);
handler.OnManipulationEnded.AddListener(HandleOnManipulationEnded);
...
/// <summary>
/// If you need it later you need to store the pointer since unfortunately in
/// OnManipulationEnded the <see cref="ManipulationEventData.Pointer"/> is null
/// (no idea why they do it this way :D )
/// </summary>
private IMixedRealityPointer _pointer;
private void HandleOnManipulationStarted(ManipulationEventData eventData)
{
_pointer = eventData.Pointer;
// whatever shall happen when manipulation started
}
private void HandleOnManipulationEnded(ManipulationEventData eventData)
{
// whatever shall happen when manipulation ended
}
Note: I am not sure if this thing you are trying to achieve is possible with this architecture ... it is very possible that nesting various ManipulationHanlder leads to strange behavior here and there. Especially very small parts will be almost impossible to grab ...

Is there any way to use AddComponent with a script in Unity if there is no instance of that script in the game yet?

I need a way to add a script to an object that I just Instantiated. Ideally I could just set the script to add in the inspector. The problem is that I can't use
public Component scriptToAdd;
because then I would need to already have an empty object or something with the script on it. While that would work it feels kind of dirty or hackish to make an empty that just stores references to something. Also the point of doing it like this for me is to make it more efficient to swap out different scripts. If I had to swap out a script on an empty and then drag the new one in I'd lose on some of that efficiency I'm after. Thanks for any help.
After you instantiate, you should use the GameObject.AddComponent() Class, to add any component Type you want.
for example, if you want to add a camera to the scene.
void Start () {
GameObject myNewCamera = new GameObject();
//creates an empty game object, you can already attach components to it.
myNewCamera.AddComponent<Camera>();
//adds a component of type Camera
}
You can also use the overloaded constructor
GameObject myNewCamera = new GameObject("my object name", typeof(ScriptToAdd));
GameObject myNewCamera = new GameObject("my object name", typeof(ScriptToAdd), typeof(MoreComponentsToAdd));
Also, you should never be afraid of using an empty GameObject on your scene, for keeping information, references, variable values, saves, or anything really. It doesn't really affect the performance of your game, and you can make your life easy just by having an easy to find tag, such as "Engine".
Then you can access anything just by adding:
GameObject.FindGameObjectWithTag("Engine");
I hope I was able to help you :)
-Noe

Unity | MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineTransform.cs:28)
Destroy+$SpawnAfter5Seconds$1+$.MoveNext () (at Assets/Scripts/Destroy.js:22)
Any help?
You are tying to perform an operation on an object which is now null because it was Destroyed.
Solution
Don't Destroy it or don't try to access something that is already destroyed. You can always check like this:
if(transformReference != null)
{
// Safe to use.
}
It means that the object you are trying to move, change position, etc. id destroyed.
Try making a new object called like the object you used in your script. If it doesn't help, post the script in a comment.
Have a great day!