Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by 686InSomNia686 · Feb 03, 2016 at 03:03 PM · c#programming

[C#] Variable init but delete after that

Hello, I am posting here cause I have a problem with some variables and I don't understand why. I am sure it's a basic question asked a million times but I didn't succeed to find any solution to my problem. This is an example of class:

 class A : Mono
 {
     bool b = false;
     float f;
     void Init ();
     {
         b = true;
     }
     public void setV(float _f)
     {
         if (b)
         {
             f = _f;
         }
     }
 }

This class is a Slider Manager, I am initializing it at the begining of my game. The Init is called by my script Main. During the game, the user can use a slider to modify the content of this float. But only if the boolean is "true", I have checked with breakpoints, "b" is well init but in the process it looses its value and back to "false". So when the slider calls the setter setV, it always fails. One explaintion could be that the class is deleted between the init and the using, but how I could avoid that? Because it's not done by myself. Thanks a lot for your help, Clément

Comment
Add comment · Show 3
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 NoseKills · Feb 03, 2016 at 06:21 PM 1
Share

$$anonymous$$ake b a property and put a Debug.Log in the setter to see if it changes and when. You can also print out the instance id of the object when changing b or f so you know if the init is initing the same object you are calling setV on.

As such i can't see anything wrong with the logic here. There are some obvious syntax errors though, that would prevent you from running this exact code and that's why I wouldn't analyse this sample too thoroughly because there migh be some other mistake in your actual script.

avatar image 686InSomNia686 NoseKills · Feb 04, 2016 at 05:46 PM 0
Share

GetInstanceID was really usefull. I have succeed to find that the class B was created twice, one in my $$anonymous$$ain class and one by Unity. I have replaced "AddComponent" by "GetComponent" to get the one created automaticly. I check with a boolean that my empty object don't pass in the update until I initialize it in my $$anonymous$$ain class. Used to work on C++, I am little bit confused by the working of Unity and its constraincts. Thanks for your help.

avatar image Taxen0 · Feb 03, 2016 at 07:25 PM 0
Share

What do you mean with "the class is deleted between the init and the using"? Is the gameObject that have the script destroyed or something like that?

if so you would have to run Init() again when it's recreated.

0 Replies

· Add your reply
  • Sort: 

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

78 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 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 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 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

How to instantiate object dependent on position 1 Answer

How can I have a life added bar every time I reach 5000 points 0 Answers

Unity2D Door and Button Pairing,Unity2D door and button pairing 0 Answers

Deck Scripting problem 2 Answers

My character slides when I press any button 0 Answers


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