Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Levantez · Oct 04, 2013 at 07:44 AM · c#assign-variable

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?
      }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Oct 04, 2013 at 08:35 AM 0
Share

You should choose the most helpful answer as the correct one. It's your duty as the OP.

avatar image Fattie · Oct 04, 2013 at 09:21 AM 0
Share

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!

avatar image Fattie · Oct 04, 2013 at 09:30 AM 1
Share

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.

avatar image Jamora · Oct 04, 2013 at 09:50 AM 0
Share

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?"

avatar image Fattie · Oct 04, 2013 at 09:52 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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++)

For more info, see this and this.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Levantez · Oct 04, 2013 at 08:29 AM 0
Share

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.

avatar image vexe · Oct 04, 2013 at 10:43 AM 0
Share

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.

avatar image Fattie · Oct 04, 2013 at 10:47 AM 0
Share

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
avatar image vexe · Oct 04, 2013 at 10:51 AM 0
Share

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.

avatar image Fattie · Oct 04, 2013 at 11:05 AM 0
Share

ah sorry - well, it is useful info always worth repeating for new chums reading in the futcha

Show more comments
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Levantez · Oct 04, 2013 at 08:30 AM 0
Share

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.

avatar image Hoeloe · Oct 04, 2013 at 08:39 AM 1
Share

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.

avatar image Fattie · Oct 04, 2013 at 09:23 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Making a bubble level (not a game but work tool) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges