var other : GameObject;
private var pwr_plus = 10;
var proximity : float = 2;
function Update () {
var dist = Vector3.Distance(other.transform.position, transform.position);
print(dist);
if (dist <= proximity)
{
gameObject.Find("player_ship").GetComponent(control_ship).power += pwr_plus;
Destroy(gameObject);
}
}
The above code is what I am trying to use to test when a player nears the energy object. It works fine and does do what it is supposed to except for one problem. For some reason the “dist” variable does not update. I need to know how to fix this.
The “other” variable is set to “player_ship”.
This script does work if the game begins with the objects within proximity. So I believe that the problem is that the variable is not updating. If you could tell me why this is I believe I could fix it myself but I have tried moving the “dist” variable in and out of the Update function and neither worked and have messed with the code for a while and nothing seemed to work. What is the problem.
Try this instead:
var other : GameObject;
private var pwr_plus = 10;
var proximity : float = 2;
var dist : float;
function Update () {
dist = Vector3.Distance(other.transform.position, transform.position);
print(dist);
if (dist <= proximity)
{
gameObject.Find("player_ship").GetComponent(control_ship).power += pwr_plus;
Destroy(gameObject);
}
}
First flance, I don’t see any big problems.
Try adding : float after dist ?
var dist : float;
dist = Vector3.Distance(other.transform.position, transform.position);
Making it two steps might help.
I didn’t really understand why the problem was occuring either.
Perhaps my ship is not moving? But that doesn’t make any sense. Does a Character Controller move the entire object? If not then that’s the problem. Maybe I should try using other moving objects?
Add :
Debug.Log ("Object position:"+transform.position);
Debug.Log ("Other position:"+other.transform.position);
Debug.Log ("Distance:"+Vector3.Distance( tranform.position, other.transform.position));
Does it show the correct / expected values ?
Did you apply that code to your “energy” object?
EDIT:
gameObject.Find("player_ship").GetComponent(control_ship).power += pwr_plus;
Destroy(gameObject);
This looks kinda strange to me. “gameObject.Find” means you are looking at its parents objects, right? That means you attatched the player_ship to the energy object? I think this might be a problem.
Also you delete the object afterwards so maybe it works at the beginning and then just deletes the energy object?
I think you should put GameObject instead of gameObject.
No the script works fine. I changed proximity to a larger number and the script executed perfectly.
I found the problem: I was selected an object out of the scene and thus it’s position was not updating (though for some reason it fed the position of the ingame object’s starting position???). Anyway I just had the object find the player ingame and do the test. Now it works, I still have no idea why it didn’t earlier though.
Thanks for the help.