script in assetbundle not loaded

hi all!
I’ve this problem: I create an assetbundle that contains a single prefab with this code:

GameObject go = Selection.activeGameObject;
string name = go.name;

Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/" + name + ".prefab");
PrefabUtility.ReplacePrefab(go, prefab);
AssetDatabase.Refresh();

but I’ve got some scripts attached to the gameobjects inside my prefab but the scripts are not loaded when I load my assetbundle… I assume that are not present in the .prefab…

how can I solve this?

thanks

I’m not sure if I understand exactly what you’re asking, but generally scripts can’t be included in assetbundles. Take a look at http://docs.unity3d.com/Manual/scriptsinassetbundles.html

1 Like

thanks, but the code above is for the ‘old’ unity 4 system, i’m using the new assetbundle management…and having only a prefab, I think that the problem is when I create the prefab

Hi again, I’ve reached to load my script in a different way, but after, using the code in the manual, I give this error:

the code is this:

TextAsset txt = codeBundle.LoadAsset(item, typeof(TextAsset)) as TextAsset;
var assembly = System.Reflection.Assembly.Load(txt.bytes);

thanks again!

The BadImageFormatException would indicate that the txt.bytes doesn’t translate to a valid assembly.

If you try outputting the txt.bytes to a file, is it identical to the original assembly?

yes, I’ve added this line:

File.WriteAllBytes(@"C:\Output.txt", txt.bytes);

and the output.txt is the same like the original assembly… :frowning:

And if you try loading the assembly directly from a file, not through an asset bundle, does it work correctly then?

Not sure what could be going on here. You can see the documentation for the BadImageFormatException here: BadImageFormatException Class (System) | Microsoft Learn

nope, also reading directly the file, the result is the same:

var bb = File.ReadAllBytes(@"C:\LayoutTick.cs");
var ass = System.Reflection.Assembly.Load(bb);

reading the guide the problem seems to be not in my text file, but for some dll that MonoDevelop uses…

ok, solved, i loaded directly a c# file, now I created a dll with the .cs and is imported

1 Like

Hi, @ghiboz
i have the same prob, can you plz brief your solution…

hi @Falcon_DZ ,
i’ve created a dll that contains the scripts and after I import the dll…
not yet tested if works right with the latest version

Thanks @ghiboz