Hi everyone.
I’m trying to store a raycast-hitted game object in a variable, so I can change the material of it later on. At the moment, Unity crashes every time it runs this particular line of code, the line that says:
selectedUnit = hit.collider.gameObject;
Here is the code:
public class SelectUnit : MonoBehaviour {
private Ray ray;
private RaycastHit hit;
public GameObject selectedUnit;
void Update () {
int i = 0;
while (i < Input.touchCount) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit, 100)) {
selectedUnit = hit.collider.gameObject;
Debug.Log ("selected unit is: " + selectedUnit.name);
}
++i;
}
}
}
}
If i omit that line of code with //, the project runs fine, but if it’s run, then Unity will crash. Can anybody work out why it crashes?