- Home /
Is it possible to delete member variable? C#
Hi,
Lets say that I have a variable. The variable only needed to be use in Awake(). And then it will never got used again.
Since having a variable take up space, is it possible to delete that variable during runtime?
Or does C# automatically do magical stuffs for us that I am not aware of?
public class foo
{
bool b = false;
void Awake()
{
if(b) DoSomething();
//b is never use again
//will b = null do the trick?
}
}
You should choose the most helpful answer as the correct one. It's your duty as the OP.
I love this question - it goes right to the heart of language design.
I'll show you a truly amazing solution to the fundamental issue you are tackling -- but you have to give me 20 bucks.
http://answers.unity3d.com/questions/541431/how-the-heck-do-you-check-if-start-is-still-runnin.html
The answer by Jessy there is one of the top 5 answers on this whole site in terms of raw cleverness. Enjoy!
The actual answer to the real sense of your question is that you do it by swapping out scripts.
This is super-common in Unity and a main part of the paradigm.
You add a script to something that's running using AddComponent, and you destroy "yourself" as a component with DestroyImmediate(this); You can google on these for examples.
So the really elegant way to do "what's in your head" is you have one component (one script, I mean) that runs during the "first part" of your thinking above. When you no longer need that messy boolean, get rid of "yourself" and ins$$anonymous$$d add the other cleaner script. Then you need no longer check that flag every loop.
It is incredibly insightful that you realised this is an issue. Actually it's amazing.
The acid test for Gifted Natural It-Can't-Be-Taught Programmer is if when you are first exposed to something like the fencepost problem you just instantly see it in your head in an obvious way. {Or my endless bugbear on here, that "unique random numbers!" shares an identity with "shuffling".}
This for me is a similar sort of "observation", that toggles can become sort of "redundant and annoying." If you really "get with" the kind of paradigm of Unity, which is sticking components (whether colliders, sounds, scripts, animations, etc) on and off GameObjects, you will feel the natural solution.
I agree; boolean flags, and surprisingly often, if-clauses altogheter, can be avoided by adherence to the OOP paradigm. I've started to get into the habit of asking myself, whenever I'm about to write an if-clause, "Is this if clause really necessary? Am I missing a behavior?"
For sure, your first sentence is an outstanding way to describe the situation.
later I'll edit this page so it looks like I came up with that
Answer by vexe · Oct 04, 2013 at 07:49 AM
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++)
Thank you for your reply,
However, the reason that bool is a public variable is because it is use in Scene Editing to deter$$anonymous$$e what to and not to do during Awake().
It will go obsolete since I will move it to the Extended Editor ins$$anonymous$$d. This question is merely out of curiosity on whether C# could do this.
Hmm, well if you $$anonymous$$UST mess with from the inspector, I guess you can't escape making it a public variable. It's O$$anonymous$$ in this case (it's just a bool, one byte of memory) - However I don't like to say that since that's not a very healthy practice. Perhaps if you tell me what you're doing with the boolean, I could give you alternative ways - @Fattie's method sounds neat, you might consider that as well.
Ahhh!
OP, stupidly in Unity, variables which are public, do default to being included in the childish "show them in the inspector" system.
To eli$$anonymous$$ate this nonsense, in c#
[HideInInspector]
in unityscript,
@System.NonSerialized
He actually wants the variable to appear in the inspector at the beginning. After he's done with his one-shot usage of it, he wants it to totally disappear, not just from the inspector.
ah sorry - well, it is useful info always worth repeating for new chums reading in the futcha
Answer by Hoeloe · Oct 04, 2013 at 07:54 AM
Use a temporary variable. When you define a variable you also define the scope of the variable. If you define it directly in the class, then the scope is the entire class, but you can also define it inside a method. A variable defined inside a method will have a scope of the remainder of that method. So, if I write this:
public class foo
{
bool b = false;
void Awake()
{
b = true;
}
void Start()
{
Debug.Log(b);
}
}
This will be fine, and will log the value "true". However, if I write this:
public class foo
{
void Awake()
{
bool b = true;
}
void Start()
{
Debug.Log(b);
}
}
I will get a compiler error, because the boolean b does not exist outside the method Awake, so when I try to read it in Start, it can't find anything with that name.
Thank you for your reply,
However, the reason that bool is a public variable is because it is use in Scene Editing to deter$$anonymous$$e what to and not to do during Awake().
It will go obsolete since I will move it to the Extended Editor ins$$anonymous$$d. This question is merely out of curiosity on whether C# could do this.
Well, the short answer is no. The long answer is yes if you wrap it up inside a class first (C# will garbage collect classes), but that has its own performance overhead, which is worse than just having the boolean sitting there.
@Levantez - it is overwhele$$anonymous$$gly important that you absolutely understand:
There is absolutely no - we mean NO, ZERO, NONE - performance problem with what you are describing. You absolutely do not have to worry about the memory or processor use in question.
So that's like "point A" here.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Error assigning to a variable 0 Answers
Error assigning to a variable 1 Answer