There’s no such thing as getting rid of a member.
Since you only need it in a certain scope, why not just declare it in that scope? (Why make it a member in the first place?)
void Awake()
{
bool b = someCondition;
if(b) DoSomething();
}
‘b’ is allocated memory on your stack, when we reach the end of Awake
’s scope, ‘b’ will die (its memory will get reclaimed) - For more info, see variable scope.
“will b = null do the trick?”
Of course not, you can’t do that, ‘b’ is a bool (value-type, which means you can’t null it) you can only null reference types. See value types VS ref types.
“Or does C# automatically do magical stuffs for us that I am not aware of?”
Well yes it does but not for value types. If you lose all references to an object allocated in the heap memory, that object will get garbage collected eventually
.
For example:
1- GameObject go = new GameObject();
2- GameObject otherRef = go;
3- go = null;
In the first line, we instantiate a new gameOjbect in the heap memory, so now our ‘go’ (which is sitting in the stack) is referencing that object.
We then create another reference, and let it reference what ‘go’ is referring to (the same gameObject, so now it has 2 references to it)
In the third line we make ‘go’ refer to null (which just means, don’t refer to anything)
so now we only have one reference to our gameObject (no garbage collection so far) - However, if we do something like:
otherRef = anotherGo;
Now, our old gameObject doesn’t have any reference to it, it’s lost, now it’s marked for garbage collection. (If you were in C++ this is called a memory leak, where something gets lost, nothing is referencing it, no garbage collection in C++)
For more info, see this and this.