- Home /
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[i] = new Color(0, 0, 0, 0);
}
/// <summary>
/// Resets the covering. The image goes back to it's original texture
/// </summary>
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[i].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);
}
/// <summary>
/// Scratchs main method. be careful, This method will scratch with interpolation if it's activated
/// See the "interpolate" attribute
/// </summary>
/// <param name="position">Position, x:y on the texture</param>
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?
Answer by phil_me_up · Feb 27, 2016 at 09:50 AM
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.
Thanks so much for deciding to help us, I will be testing what you explained now and get back to you if it solves our problem. Thanks again!
Hey Phil I think you may be right! Debugging this how you said too, I can see now in Xcode that "current" never gets assigned and that is why we are getting the BAD_ACCESS error. Any idea on what I should do so that it registers that? On Unity "current" is assigned. But on Xcode "current" is not assigned. What should I do to assign it? Thanks so much!
We'll I'd start by double checking the order your functions are called. You might find that just safety checking as I described above, or wrapping in a try...catch statement will solve the problem (in the case that the function is called before initialisation is complete).
Another method I'd to expand the safety check and if current us found to be null, assign it as needed for your project.
Unfortunately I can't really tell you how to assign it as I'm not sure what it is your assigning and why. You might actually be better off investigating why this reference becomes null in the first place and prevent that from occurring (however, safety checks are always very much recommended anyway - they help keep your game stable!)
Thanks so much for your help, now that I know this is the problem we can continue debugging (trial and error) style to figure out how to solve this. Thanks again for your time!
Your answer
![](https://koobas.hobune.stream/wayback/20220612065752im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My app file size doubles that of what the console says it should... 1 Answer
Mix native Objective-C code and Unity3d 0 Answers
xcode build fail - Mach-o linker error 3 Answers
AWS in iOS not working 2 Answers