- Home /
Why can't I put transform.translate into a function in unity? (javascript)
I am trying to create a function which issues specific transform.translate commands to move game pieces.
it goes something along the lines of:
static var MoveUpleft = function () {
if (Time.deltaTime < 1) {
transform.Translate(x * Time.deltaTime,y * Time.deltaTime,z * Time.deltaTime,Space.World)
}
};
and a logic system like:
static var MoveUpLeft = false;
if (condition == true) {
MoveUpLeft = true
}
and on the thing that needs to move:
function Update() {
if (MoveUpLeft == true){
if (Time.deltaTime < 1) {
MoveUpleft();
}
MoveUpLeft = false;
}
}
The problem that I am getting is that there is a compiler error (meaning that there is already a problem before the game can even be run) , saying that "transform" is an unknown identifier. I ran a test script with transform.translate directly under update, and not by use of the function, and the problem went away. Does this mean that you cannot use transform.Translate in a function?
How about other alternatives for motion, like addForce?
This problem has been bugging me for days now, so thanks in advance!
I am complete beginner in using unity, or coding in general, as I only knew about unity for around half a year, and haven't devoted much time into it, so sorry if this question sounds stupid!
As an additional note, can Time.deltaTime be put in an if statement like that? Does it reset every time it gets called? How does it work?
Answer by doublemax · Oct 16, 2016 at 10:14 AM
static var MoveUpleft = function () {
A static method is not connected to any class instance, so there is no "transform" available. Do you really need/want a static method here? Probably not.
As an additional note, can Time.deltaTime be put in an if statement like that? Does it reset every time it gets called? How does it work?
Time.deltaTime is the time since the previous frame was rendered. It's updated every frame.
If the OP does want a static method (which, I agree, is unlikely), they could always pass the Transform component in question as a parameter to the $$anonymous$$oveUpLeft method:
static var $$anonymous$$oveUpleft = function ( transform : Transform) {
transform.Translate(x * Time.deltaTime,y * Time.deltaTime,z * Time.deltaTime,Space.World)
}
I tried making the function not static, but the same complier error
"BCE0005: $$anonymous$$ identifier: 'transform'." occurs.
This is my exact code:
public class directions {
static public var $$anonymous$$ovecounter = 0;
var right : float = Time.deltaTime 1.602389; var up : float = Time.deltaTime 2.77544987; var left : float = Time.deltaTime -1.602389; var down : float = Time.deltaTime -2.77544987;
var $$anonymous$$oveUpright = function () {
transform.Translate( right, up, 0 , Space.World);
};
Sidenote: I have mistaken what Time.deltaTime was. Thank you for infor$$anonymous$$g me :)! What I wanted was a Time.time that could reset, and I just assumed Time.deltaTime was Time.time without research.
However, the method suggested by tanoshimi seems to work. Thanks!
Again, thanks for the quick replies!
Why do you actually create an anonymous method? That's the main problem here. Also you initialize it in a field initializer. Field initializer are executed even before the constructor that's why this can't never work, no matter if it's a static or an instance variable.
Just create a normal class method:
function $$anonymous$$oveUpright()
{
transform.Translate( ... );
}
You can't use Time.deltaTime in a field initializer as well. Also it wouldn't make any sense. DeltaTime might be different each frame and that's actually the main reason why you would use it at all. If you just assign the value once it would be a constant.
This wouldn't make much sense in an actual Javascript environment either. $$anonymous$$eep in $$anonymous$$d that Unity doesn't use Javascript. Unityscript is just a .NET language that has a syntax similar to Javascript but in fact has nothing to do with Javascript. It's a completely different language.
Your answer