Xcode Problem (Whats Wrong With My Code)

So we are creating a scratch card type effect in our app. This app is already out on android and the effect is working perfect. Now we are trying to get it on iPhone, but we keep running into a error with the scratch card effect on Xcode. It works on Unity editor and on Unity Remote for IOS but it won’t do the effect when on the device alone. Heres our Scratch Card Effect code. The build is always successful, the scratch card effect just does not work.

using UnityEngine;
using System.Collections;

public enum RefreshRate
{
    _10_FPS = 10,
    _20_FPS = 20,
    _30_FPS = 30,
    _40_FPS = 40,
    _50_FPS = 50,
    _60_FPS = 60,
}

public class ScratchIt : MonoBehaviour
{
public bool interpolateMovement = true;
public int brushSize = 30;
public RefreshRate refreshRate = RefreshRate._30_FPS;
[SerializeField, Range(10, 500)] int maxCycleByInterpolation = 100;
[SerializeField, Range(1, 10)] int alphaCheckPrecision = 5;
[SerializeField] float scratchCompleted;
public float ScratchCompleted
{
    get {return scratchCompleted;}
}
Color[] brush;
Color32[] data;
Texture2D tex;
float timer;
Vector2 oldPosition;
RenderTexture rt;
Texture2D current;

void Start ()
{
    oldPosition = Vector2.zero;
    timer = 0;
    current = renderer.material.GetTexture(0) as Texture2D;
    ResetCovering();
    int tabSize = current.width * current.height;
    brush = new Color[tabSize];
    for (int i = 0; i < tabSize; i++)
        brush *= new Color(0, 0, 0, 0);*

}

///


/// Resets the covering. The image goes back to it’s original texture
///

public void ResetCovering()
{
data = current.GetPixels32();
Texture2D newTex = new Texture2D(current.width, current.height);
newTex.SetPixels32(data);
renderer.material.SetTexture(0, newTex);
tex = newTex;
tex.Apply();
updatePercentage();
}

void Update ()
{

if (ScratchCompleted >= 0.6) {

Debug.Log(“YOU WON!!”);

}

timer += Time.deltaTime;
if (Input.GetMouseButton(0))
{
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Physics.Raycast(r, out hit);
try
{
if (hit.collider.gameObject == this.gameObject)
{
if (!interpolateMovement)
scratch(new Vector2(hit.textureCoord.x * tex.width, hit.textureCoord.y * tex.height));
if (timer > (1f / (float)refreshRate))
{
if (interpolateMovement)
scratch(new Vector2(hit.textureCoord.x * tex.width, hit.textureCoord.y * tex.height));
updatePercentage();
tex.Apply();
timer = 0;
}
}
}
catch {
}
}
}

public void updatePercentage()
{
Color32[] tmp = tex.GetPixels32(); // __________ SLOW __________
float value = 0;
for (int i = 0; i < tmp.Length; i += alphaCheckPrecision)
value += tmp*.a;*
scratchCompleted = 1 - value / (255 * data.Length / 5);

}

void scratchInterpolated(Vector2 position)
{

if (Input.GetMouseButtonDown(0))
oldPosition = position;
int i = 0;
Vector2 tmpPos = oldPosition;
Vector2 dir = (position - tmpPos).normalized;
if (Vector2.Distance(tmpPos, position) > maxCycleByInterpolation)
{
oldPosition = position;

}
else while (Vector2.Distance(tmpPos, position) > 2)
{
oldPosition = position;
scratchPoint(tmpPos);
tmpPos += dir;
i++;
if (i > maxCycleByInterpolation)
{

break;
}
}
}

public void scratchPoint(Vector2 position)
{
Vector2 tmpPos = position - new Vector2(brushSize / 2, brushSize / 2);
Vector2 cl = new Vector2((tmpPos.x + brushSize) - tex.width,
(tmpPos.y + brushSize) - tex.height);
tmpPos = new Vector2(Mathf.Clamp(tmpPos.x, 0f, tmpPos.x),
Mathf.Clamp(tmpPos.y, 0f, tmpPos.y));
Vector2 tmpBrushSize = new Vector2(Mathf.Clamp(brushSize, 0, brushSize - cl.x + 1),
Mathf.Clamp(brushSize, 0, brushSize - cl.y + 1));
if (tmpPos.x <= 0)
tmpBrushSize.x = position.x + brushSize / 2;
if (tmpPos.y <= 0)
tmpBrushSize.y = position.y + brushSize / 2;
tex.SetPixels((int)tmpPos.x, (int)tmpPos.y, (int)tmpBrushSize.x, (int)tmpBrushSize.y, brush, 0);
}

///


/// Scratchs main method. be careful, This method will scratch with interpolation if it’s activated
/// See the “interpolate” attribute
///

/// Position, x:y on the texture
public void scratch(Vector2 position)
{
if (interpolateMovement)
scratchInterpolated(position);
else
scratchPoint(position);
}

public void scratch(Vector3 position)
{
scratch(new Vector2((int)position.x, (int)position.y));
}
}
On the Xcode Debugger it says the error is in our Assembly-CSharp.dll.s and the error it says is EXC_BAD_ACCESS (code=1, address = 0x0)
When we further debugged it, we pin pointed the code breaking when it reaches the public void ResetCovering() and hits the data = current.GetPixels32(); line.
Is GetPixels not readable by Xcode? Anything look wrong in my code? Is there a framework I am missing?

Only briefly looked at the code as I’m on my phone, but I’d say your problem would be that ‘current’ might not assigned at the point you hit that function, and hence there is nothing to GetPixels on.

You really should be checking the validity of current and other references before you try using them, so stick a ‘if(current != null)’ in there.

You should be able to debug this in mono too, just check of that variable is null or not the first time that line is hit.