Textures problem

I have this code, it worked fine in regular Unity but noy with Unity iPhone, even just in development window:
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
var theIn = Random.Range(0,3);
var theTexture = arrayTexts[theIn];
sphere.renderer.material.mainTexture = Resources.Load(theTexture);

I’ve tried loading the textures individually by name, setTexture, everything I could think of, but nothing working, what’s going on?

Thanks.

What does the editor log mention which very likely shows errors.

Generally the problem is pretty surely related to you relying on automagic which does not exist on the iphone.

That’s the thing I’m not getting any errors. What is automagic?

“automagic” is what takes care that JS users don’t have to know what actually happens behind the scene if they use properties or when it comes to type casting, because JS does it automagically behind the scene.

Also dynamic typing is not present on the iphone, so if a function returns Object and you assign it to a variable without a type it will be an Object, it won’t become a GameObject or a Transform or a Component (or even a specific MonoBehaviour) if you try to access specific fields that only exist on them. you need to cast things to the correct class through “xxx as NewClass” with NewClass beeing the name of the class you want it to cast too and xxx beeing a reference to an object that is of NewClass too but currently held by a more variable reference of a more generic class (from which NewClass inherits)

Well I found that this works:

Resources.LoadAssetAtPath(“Assets/Blue.jpg”, Texture2D)

Although “Blue.jpg” is already in the Assets folder and I thought Resources.Load the default folder was Assets (?).

I did change the location of my Project folder earlier, when I had problems getting my App to work on the iPhone, so I read on the forums to move your project folder to Shared/Unity/MyProject I forget what the default folder it was under, I suppose, Unity_iPhone/

Obviously new to Unity. But I would like to ask a question, “xxx as NewClass” is that for JavaScript iPhone also?

Thank you.

Resources Default folder is the Assets\Resources folder, the only folder thats always included in the build.

loading something outside thats not used in any scene through a direct assignement will result in a crash as it isn’t present on a build.

And yes, the xxx as NewClass is the type casting for JS, independent if iPhone or Desktop (on the desktop you don’t need it though)