• 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
1
Question by Aubrey-Falconer · Dec 10, 2009 at 07:03 AM · errorassetjavascript-specificinvalidcastexception

BuildAssetBundle results in System.InvalidCastException

My code is posted below. You pass it a directory, and it should package all the assets in the directory into an AssetBundle. Instead, it crashes with an "System.InvalidCastException: Cannot cast from source type to destination type." error on the "BuildPipeline.BuildAssetBundle" line.

If I call "BuildAssetBundle(objs[0], null", everyt$$anonymous$$ng works great. If I call "BuildAssetBundle(objs[any_number_up_to_length_of_array], null", everyt$$anonymous$$ng works great. I can't figure out why Unity doesn't like my Object[] array, and why it won't let me build multiple assets into a bundle instead of just one at a time...

//Directory of assets to package into bundle var ab : String = "Assets/Path/To/Assets/To/Package/";

//Determine title for t$$anonymous$$s bundle var offset : int = ab.Substring(0, ab.length-2).LastIndexOf("/") + 1; var title : String = ab.Substring(offset, ab.length - offset - 1) + ".unity3d";

//Iterate through each file to be built in t$$anonymous$$s bundle var objArr : Array = new Array(); var files : String[] = System.IO.Directory.GetFiles(ab, "*", System.IO.SearchOption.AllDirectories); for(var file : String in files) objArr.Add(AssetDatabase.LoadMainAssetAtPath(file));

//Build Bundle! var objs : Object[] = objArr.ToBuiltin(Object); BuildPipeline.BuildAssetBundle(null, objs, title);

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
2
Best Answer

Answer by Aubrey-Falconer · Dec 10, 2009 at 07:25 PM

NCarter just solved t$$anonymous$$s for me on the #Unity3D chat!

"Object" by default references System.Object and not UnityEngine.Object.

Explicitly referencing UnityEngine.Object resolved the issue.

T$$anonymous$$s is either a bug or a very strange design decision in Unity's JS implementation...

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 Aubrey-Falconer · Dec 10, 2009 at 05:43 PM

Here's some code simplified as much as it could possibly be - I just don't understand how it can fail to run properly.

Debug.Log(file); //Assets/W$$anonymous$$rlds/MarsAssets/Textures/Dirt.png Debug.Log(title); //Textures.unity3d

var objs : Object[] = new Object[1]; objs[0] = AssetDatabase.LoadMainAssetAtPath(file); BuildPipeline.BuildAssetBundle(null, objs, title); //System.InvalidCastException: Cannot cast from source type to destination type.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Asset server error - "no pg_hba.conf entry" 2 Answers

Unity says that my script isn't doing anything? 1 Answer

grab weapon script problem 1 Answer

An issue with the chronos plugin 0 Answers

Problem with custom shaders (surface function error) 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