Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 joseadvrodrigues · Dec 14, 2020 at 04:22 PM · gameobjectprefabserializationreferencereload

Why is my prefab instance loosing a GameObject's reference on script reload?

I have an [ExecuteAlways] component with a GameObject field and on enable, if the field is null, it creates a new GameObject and assigns the field to it:


 using UnityEngine;
 
 [ExecuteAlways]
 public class TestScript : MonoBehaviour
 {
     bool changeTheNameOfThisFieldToReloadScript;
     public GameObject go;
 
     private void OnEnable()
     {
         if(go == null)
         {
             go = new GameObject();
             go.name = go.GetInstanceID().ToString();
         }
     }
 
     private void OnDisable()
     {
         if(go != null)
         {
             DestroyImmediate(go);
         }
     }
 }
 



Online video 1 (click here to watch): I create a new GameObject and attach this script to it. It works fine: entering play mode doesn't create a new game object if one was already created. The same happens after reloading the scripts (to reload the scripts, I just changed a field's name).


Online video 2 (click here to watch): I now turn the original GameObject into a prefab. It works fine at first; however, when I reload the scripts and enter play mode, the "go" field becomes null and a new GameObject is created! Instead of having one GameObject created by the TestScript, I now have two.


Why is the "go" field null after reloading and hitting play? And why does this only happen when the object is a prefab instance? Is Unity unable to serialize it? Please tell me if you need more information. You can download the project here (67.07 KB).

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

2 Replies

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

Answer by joseadvrodrigues · Dec 18, 2020 at 12:31 PM

From forum user MaskedMouse:

When you set something through script you have to mark the object as "Dirty" so that Unity knows this has to be saved.

Do you need the ExecuteAlways? If so, mark the game object as dirty. if not, remove ExecuteAlways.

ExecuteAlways will execute OnEnable / OnDisable even though the player is not in a playing state. Any changes made need to be marked as Dirty.


To fix my problem I just had to call EditorUtility.SetDirty(this) after creating the new GameObject. The new OnEnable method became:

 private void OnEnable()
 {
     if(go == null)
     {
         go = new GameObject();
         go.name = go.GetInstanceID().ToString();
         EditorUtility.SetDirty(this);
     }
 }
Comment
Add comment · 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
0

Answer by Casiell · Dec 15, 2020 at 08:57 AM

The gameobject you are referencing is an object on the scene. Prefabs CANNOT have references to objects on scene. Only to objects that are inside their own hierarchy.

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 joseadvrodrigues · Dec 15, 2020 at 09:49 AM 0
Share

I'm not trying to reference the scene object in the prefab asset, but in the prefab instance (called "GameObject" in the scene).

In the Online video 2 in the first six seconds you can see this: in 0:03 you can see that the prefab asset has a null reference in the inspector and in 0:06 you can see that the prefab instance in the scene has a valid reference to the other object in the scene.

And it does get properly serialized. In the same video, when I hit play for the first time, it works properly (doesn't create a new game object because the "Go" field is not null). The problem comes after reloading the scripts and hitting play for the second time: the prefab instance looses it's reference, then on enable it creates another game object.

Why doesn't the prefab instance serialization work on script reload?

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

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

How to save/load references to components and prefabs 1 Answer

NetworkReader/Writer with GameObject 0 Answers

How do I assign a prefab reference to a private variable? 1 Answer

Disable GameObject through Prefab? 2 Answers

Holding references to other gameObjects 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