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 /
avatar image
0
Question by VadDivine · May 04, 2020 at 12:54 AM · runtimemethodreturn

why does return values method runs multiple times when it is called only once?

Hello. i just want to understand how the code below run. i cant figure out why the return value method called multiple times on runtime or exit runtime.

  • i am using visual studio to edit script

  • code is written in c#

  • using unity for my game developement

this is my example

 public static class R1
 {
       public static int N1;
       public static int GetN1()
       { 
              Debug.Log("GetN1");
              Debug.Log(N1);
              N1++;
              Debug.Log(N1);
              Return N1;
        }
 }
 
 public class Tester : Monobehaviour
 {
         private int A = R1.GetN1();
         private void Awake()
         {
               Debug.Log("Awake");
               Debug.Log(A);
          }
         private void Start()
         {
               Debug.Log("Start");
               Debug.Log(A);
          }
         private void OnEnable()
         {
               Debug.Log("Enable");
               Debug.Log(A);
          }
         private void OnDisable()
         {
               Debug.Log("Disable");
               Debug.Log(A);
          }
 }

when i run the code the outputs are

 GetN1
 0
 1
 GetN1
 1
 2
 Awake
 2
 Enable
 2
 
 //when i disable the game object (from unity inspector)
 Disable
 2
 
 //when i enable the game object
 Enable
 2
 
 //same goes when i disable or enable the script (Tester)
 
 //but when i exit unity player(means stop running the code in unity)
 //the output is
 Disable
 2
 GetN1
 2
 3
 

from what i understand i only called GetN1 method once, but from the output it seems to be called when i start runtime and when i exit runtime.

hope someone can enlighten me.

thank you in advance:)

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · May 04, 2020 at 01:06 AM

You call your GetN1 method from a field initializer:

 private int A = R1.GetN1();

Field initializers are executed even before the constructor of an object is executed. This happens whenever the object is created. When you enter or leave playmode the whole managed environment is serialized, destroyed, reloaded and deserialized. The objects do exist during edit time just as they exist during runtime. However when you switch between edit and play mode all objects are actually recreated.


For MonoBehaviours you should generally avoid executing code from the constructor or field initializer. That's because the object creation of components is actually done on a seperate thread (loading thread). Since most of the Unity API is not thread safe you can easily run into problems.


However for your specific case I don't really see any issues. Creating a unique ID for a session should work just fine that way. Keep in mind that when testing in the editor your scene might get saved / reloaded even several times. The additional calls you see when you exit playmode is most likely due to the reloading of the serialized scene that is currently loaded inside the editor.

Comment
Add comment · Show 1 · 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 VadDivine · May 05, 2020 at 02:29 PM 0
Share

"When you enter or leave playmode the whole managed environment is serialized, destroyed, reloaded and deserialized."

thanks for the clarification and the advice:)

this generally did not really cause any problems but its good to know.

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

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

Variables are assigned but it returns null 2 Answers

Method returning null, when referenced from another script 0 Answers

GUI calling function with return value. I dont know why this doesnt work. 0 Answers

Stop calling a function 1 Answer

Export objects to a .3DS file at runtime 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