I have a controller script that figures out what a player is touching on the screen. Then I have a script that moves the lid of a coffin if the player touched it. However, whenever I try any code – the function intended or even just updating a boolean variable – there is an error in XCode that breaks the app. For the life of me I can’t figure out what is wrong.
I can confirm that without the line that “speaks” to the clickable.js script (Attached to the coffin lid) no errors happen. I can also confirm that the function on clickable.js works perfectly (just tried it with an automated movement after a 5 second delay, worked fine).
Here is my code, any assistance would be wonderful:
controller.js
function Update () {
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
var hitWorld : RaycastHit;
var screenPosWorld = Input.GetTouch(i).position;
var worldPosWorld = playerCamera.ScreenToWorldPoint(screenPosWorld);
var rayWorld : Ray = playerCamera.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (rayWorld, hitWorld, 100))
{
var playerClickDistance = Vector3.Distance(hitWorld.collider.gameObject.transform.position, playerObject.transform.position);
if (hitWorld.collider.gameObject.tag == "Clickable")
{
//I BELIEVE THIS LINE BELOW IS THE PROBLEM
hitWorld.collider.gameObject.GetComponent(clickable).GotClicked(playerClickDistance);
}
}
}
}
}
clickable.js
function GotClicked(playerClickDistance : float)
{
if (objectType == "Coffin 1" && playerClickDistance <= playerDistance) { ClickCoffin1(); }
}
function ClickCoffin1 () {
var i = 0.0;
var rate = 1.0 / animationTime;
while (i < 1.0) {
i += Time.deltaTime * rate;
coffinLid.transform.position = Vector3.Lerp(startPosition, endPosition, i);
coffinLid.transform.eulerAngles = Vector3.Lerp(startRotation, endRotation, i);
yield;
}
}