How to destroy a class in C# - class

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.

Related

Is it safe to destroy null

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

Options to storing data in a prefab

After realizing that you can't really use UnityEvents in Scriptable Objects (they don't read global variable values correctly). I had to move to another solution for where to store my Dialogs.
The best solution I could find was to just store them in the corresponding NPC-prefab. This is really convenient. However this leaves a bad taste in my mouth, it just feels wrong to store data in a prefab like this. Is it a bad practice?
For example if I were to refactor something in the DialogObject, everything would be lost.
Since I can't seem to successfully store UnityEvents anywhere (can't serialize them as Json and as mentioned Scriptable Objects don't seem to handle them well) I feel like this is the only solution if I want to be able to use the Editor to create Dialogs.
Just checking here first, is this stupid? is there another way?
I am trying to save a List of this:
[System.Serializable]
public class DialogObject {
public List<PageData> conversations = new List<PageData>();
public UnityEvent postEvent; //Invoked on last page of Dialog
}
I would say yes, prefabs are meant to be a template to create new items of a type. You are trying to save data states.
Even though you cannot serialize the UnityEvent, you could serialize its content.
If you assign via inspector, you can use https://docs.unity3d.com/ScriptReference/Events.UnityEventBase.GetPersistentMethodName.html
But then you would not have problem of storage if you know from the start where it goes.
Second solution is when you assign the method to the UnityEvent, store it.
void AddListener (UnityAction ev)
{
MethodInfo mi = ev.Method;
// Use mi
postEvent.AddListener(ev);
}
With the mi reference, you have the name of the method, its type, the object it belongs to and anything you need.

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

How to actively delete a Dart object?

I'm building a Dart app which contains a variety of class objects. The particular class object I'm dealing with contains a variety of stream event listeners on DOM elements. When I remove these objects from the DOM and untrack the class object these listeners persist.
I know that Dart runs garbage collection eventually, but I'm not even 100% sure it will come along and delete these class objects since there is a Watcher and Stream listeners that continue.
My question is, is there a way to actively delete a class object immediately? I tried setting the class object to null but that doesn't seem to work for some reason. When I check if the object exists afterward with a print statement, it still lists it as an instance of that class object.
Furthermore, for what I'm trying to accomplish, canceling streams doesn't seem to be enough. I need to destroy the class object.
Setting references to null is all you can do. Your test seems very weird. How can you print the object if you don't have a reference? If you still have a reference, how can you expect the instance to be collected.

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!