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 dmm_jbw · Apr 14, 2016 at 06:35 PM · componentmonobehaviourinitializationonpostprocessallassets

Component not added to model?

Ok, so I have a component that contains a data class. It looks something like this:

 [Serializable]
 public DataClass
 {
     public string someData = "";
     public DataClass() {}
 }
 
 public class MyDataComponent : MonoBehaviour
 {
     public DataClass myData = new DataClass();
 
     public void Update() {}
 }

Now, I'm trying to add this component in the asset processor as certain models get imported.

 void OnPostProcessModel(GameObject go)
 {
     MyDataComponent foo = go.GetComponent<MyDataComponent>();
     if (foo == null)
     {
         foo = go.AddComponent<MyDataComponent>();
     }
 
     // assume we init foo with some data
 }
 
 public static void OnPostprocessAllAssets(string[] importedAssets, ...)
 {
     // assume we get the same asset as above
     string modelPath = importedAssets[0];

     GameObject model = (GameObject)PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath(modelPath, typeof(GameObject)));
 
     MyDataComponent foo = model.GetComponent<MyDataComponent>();
 
     if (foo == null)
     {
         // FOO IS ALWAYS NULL
     }
 }

This seemed to work for me when the contents of DataClass were directly in MyDataComponent, but now that I've encapsulated them, suddenly it doesn't work again.

I can't figure out why the component returns null now when it was valid before.

Thoughts?

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

Answer by dmm_jbw · Apr 14, 2016 at 07:49 PM

Ok, got this sorted.

As it turns out there is nothing wrong with the code. The reason it worked before and then broken when I encapsulated the data is likely due to how Unity manages it's assets and stores things in its database.

The fix was to blow away the Library folder and let it rebuilt.

I had changed my script from being called DataClass to MyDataComponent and then created a new class called DataClass that was now inside MyDataComponent. This clearly messed with Unity's head!

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

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

55 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

Related Questions

How to create static component? 0 Answers

How to make RequireComponent list possible options. 1 Answer

accessing a script using a variable with getComponent, then accessing a variable inside that script 1 Answer

Public MonoBehavior string fields empty 0 Answers

How to automate removing non-MonoBehaviour derived scripts from a prefab 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