- Home /
What is wrong with this script? Error connecting with another objects scripts/functions
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;
}
}
The code below works only until access to the other script is needed:
var player : GameObject = hitWorld.collider.gameObject;
print ("GameObject Name: " + player.name);
print ("GameObject Tag: " + player.tag);
print ("GameObject Variable 'gotClicked': " + player.GetComponent(clickable).gotClicked);
XCode Output:
GameObject Name: Coffin Lid
GameObject Tag: Clickable
(lldb)
Answer by infinitypbr · Jun 25, 2013 at 12:24 AM
I was expecting an answer showing how dumb I am at times, and sure enough: The script I was trying to access was on the parent of Coffin Lid, not on the object, and that is where the problem came from.
Your answer
Follow this Question
Related Questions
Initializing objects with another scripts variables. 1 Answer
Instantiated Object's Scripts not enabled? Not sure why 3 Answers
how to build & populate an array available to all scripts on all objects? 1 Answer
Getting a Script without setting a variable to the script name 1 Answer
Adding an array of scripts to a class 0 Answers