- Home /
Get nearest object and find direction problem
Trying to find the nearest object and then get the direction from that object to the player. Keep getting the error "'transform' is not a member of 'Object'." which I don't understand because the object it refers to is being set to a GameObject which should have transform available to it!
function FixedUpdate() {
if (EQ&&thrown) {
//Objects to get direction
var player = GameObject.Find("FPSController");
var target = GetNearestTaggedObject();
//Vector from target to player
var heading = (player.transform.position - target.transform.position);
var distance = heading.magnitude;
var direction = heading/distance;
//Push player away from target
player.transform.Translate(direction * .5 * Time.deltaTime, Space.World);
}
}
function GetNearestTaggedObject() {
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
var nearestObj = null;
for (var obj : GameObject in taggedGameObjects) {
var objectPos = obj.transform.position;
var distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
nearestObj = obj.transform;
nearestDistanceSqr = distanceSqr;
}
}
return nearestObj;
}
On line 27, I'm assu$$anonymous$$g you mean to put 'player.transform.postition' ins$$anonymous$$d of just transform.position, right?
umm tbh I copied the GetNearestTagedObject from another questions answer. I don't think it should need to be player.transform.position since its setting that to one of the tagged objects. But then again I'm a novice and have no idea what I'm doing...
If you just say transform.position, it will calculate the position of the object the script is attached to. Since you had to find the player object in update, the script is not attached to the player object. If it is, this ma go without saying, but you don't need to find the player object just call it gameObject.transform...
Answer by aldonaletto · Jul 31, 2013 at 03:31 AM
You should declare the variable types. If not mentioned, JS tries to infer the type from the value you're using to initialize the variable - if there's no initialization value, the variable is assumed to be Object, which can hold any type. In this particular case, the problem probably is caused by this line:
var target = GetNearestTaggedObject();
GetNearestTaggedObject has no return type, neither the type of target is explicitly declared, thus it's assumed to be Object. Change this line to:
var target: Transform = GetNearestTaggedObject();
You should also declare the return type of GetNearestTaggedObject and the type of nearestObj:
function GetNearestTaggedObject(): Transform { // declare the return type
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
var nearestObj: Transform = null; // null doesn't define the type
...
As a rule of thumb, always declare the type of any variable that doesn't have an initialization value. Declare also the type of variables initialized with untyped values (like nearestObj - null has no specific type).
Wow that worked. I had that to begin with but removed it because it was giving me errors. Guess I needed to set my variable like the function not the function to the variable. Thanks!
Answer by highpockets · Jul 31, 2013 at 03:29 AM
Actually I missed that the player object is declared in the update function, but not in the other. Put line 5 where you declare your player object outside of the update function so both functions can use it and put what I mentioned in my last comment on line 27. Hope it works for you.
error 'transform' is not a member of 'Object'. on line 9 still
specifically on the target variable. The player variable doesn't have errors