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 Cyrille-Paulhiac · May 19, 2011 at 08:32 AM · playstartawakeexecuteineditmode

ExecuteInEditMode Component construct "twice" and "reset" custom classes

Hello community. I have an independant class, and I want, from Editor, create a component with a member in this type. It works well, but when I click on "Play Button", custom classes AND private members are reset, like the RAM were flushed by garbage collector of something. I manage to reproduce this behaviour in an EMPTY project, just add this two script files:

In a MyClass.cs :

 #if UNITY_3_3
 using UnityEngine;
 using UnityEditor;
 #endif

 public class MyClass
 {
     public string Name;

     #if UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3
     [MenuItem("Custom/InitComponent")]
     static public void MInitComponent()
     {
         GameObject MyCubeGO = GameObject.CreatePrimitive(PrimitiveType.Cube);

         MyComponent myComp = MyCubeGO.AddComponent<MyComponent>() as MyComponent;
         myComp.MyPublicBool= true;
         myComp.MyClassInstance.Name = "John Doe";
     }
     #endif

}

And in a MyComponent.cs :

 using System;
 using System.Collections;


 #if UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3
 using UnityEngine;

 [ExecuteInEditMode()]
 public class MyComponent : MonoBehaviour 
 {
     private bool MyPrivateBool;
     public bool MyPublicBool;
     public MyClass MyClassInstance;

     public bool AlreadyConstructed=false;

 // Use this for initialization
 void Awake() 
 {
     if (!AlreadyConstructed)
     {
         Debug.Log("Awaking...");
         MyClassInstance = new MyClass();
         MyPrivateBool = true;
         MyPublicBool = true;

         AlreadyConstructed = true;
     }
 }
 
 // Update is called once per frame
 void Start() 
 {
     Debug.Log("Starting...");
     Debug.Log("MyPublicBool is at value =" + MyPublicBool);
     Debug.Log("MyPrivateBool is at value =" + MyPrivateBool);// <==== OK(true) In  Editor, RESET (at false) when cliking on PLAY !

     Debug.Log("My Name is ="+MyClassInstance.Name); // <==== OK In Editor, CRASH when cliking on PLAY !
     MyPrivateBool = false; //Checking the reset here too.
 }
 }
 #endif

As a result , I have for output :


Awaking... Starting... MyPublicBool is at value =True MyPrivateBool is at value =True My Name is =John Doe

//here I click on PLAY Starting... MyPublicBool is at value =True MyPrivateBool is at value =False //WHICH IS WRONG NullReferenceException: Object reference not set to an instance of an object at MyComponent.Start () [0x0003e] in C:\UNITY\Debug01\Assets\MyComponent.cs:37

(Filename: Assets/MyComponent.cs Line: 37) // it this line: Debug.Log("My Name is ="+MyClassInstance.Name);

How can I avoid this kind of behaviour, to keep my initialisation also when I click on "PLAY" ? Thank you for your help!

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

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

1 Person is following this question.

avatar image

Related Questions

Is there a 'Play On Awake' function for animations? 1 Answer

Cached references set in awake aren't persisting 0 Answers

Invoke not working from Start() ? 1 Answer

Debug.Log print order, or Awake vs Start issue? 1 Answer

when loading a scene, the execution order is load->constructor->finishfunction->awake->start? 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