App Crashes while creating a TextMesh game object in iOS device

I have a function that will create a game object with TextMesh component dynamically, the code is as the following:

// this will create a TextMesh game object in the passed parent transform
public static FlyingText getInstanceWithTextMesh(string text, Vector3 pos, int fontSize, Color color, float speed, float duration, Transform parentTransform) {
	pos.z = parentTransform.position.z;

	// create game object
	GameObject obj = new GameObject ("FlyingText");
	obj.transform.parent = parentTransform;
	obj.transform.position = pos;
	obj.transform.localScale = new Vector3 (1, 1, 1);
	// add TextMesh component
	TextMesh t = obj.AddComponent<TextMesh> (); // !!!!!!!!!! it will crash here !!!!!!!!!!!!!!
	t.text = text;
	t.alignment = TextAlignment.Center;
	t.font = Resources.Load ("Fonts/"+Const.DEFAULT_FONT_NAME) as Font;
	t.fontSize = fontSize;
	t.color = color;
	t.characterSize = 0.1f;


It will crash at this line every time 100%:

TextMesh t = obj.AddComponent ();

Here is the crash log in Xcode:

There is absolutely no problem when I tested in the PC emulator. It only happens in iOS device(Android seems not happen this). One more strange thing, If I insert a TextMesh game object by editor(not by script code) and test again. Then it won’t crash any more. I absolutely don’t know why…

Does anyone can help?

BTW, I am using Unity 5.2.1f1 and tested in iPhone 6, iOS 8.3.

@firestoke – got a reply back from Unity and not sure if this applies to you but it involves IL2CPP and code stripping.

My sample project sent to them involved nothing but a MonoBehaviour adding a TextMesh component in the Start() method. I built the sample with IL2CPP backend and code stripping enabled. Their code stripping routines determined that I didn’t use that component as they don’t scan source to determine what components you need.

Here’s his suggestions for fixing (option 3 is my preferred choice):

We have three possible work arounds
for this issue:

  1. You can disable the “Strip Engine Code” option. This will increase the
    size of the output binary, but it is
    the simplest work around.

  2. You can add a MeshRenderer component to a dummy game object in a
    scene which is part of the build. This
    will prevent the MeshRenderer
    component from being stripped.

  3. You can use a link.xml file to tell the code stripping system to preserve
    the MeshRenderer component. This
    link.xml file worked for me:

    <assembly fullname="UnityEngine">
       <type fullname="UnityEngine.MeshRenderer" preserve="all"/>

More details about the link.xml file
are available here:
Unity - Manual: Optimizing the size of the built iOS Player

Hope that helps.