- Home /
Transform position to a public variable
I'm currently trying to make an object drop at a certain speed that is changed constantly throughout the game. I've used a public variable in another script. When the code is run, the object just sits there doing nothing. What should I be doing instead?
#pragma strict
var Turn : float;
function Update () {
transform.position.y=transform.position.y-Start.Speed;
transform.Rotate(0, 0, 1*Turn);
}
Answer by Pangamini · May 27, 2013 at 10:42 AM
In JS you can't have more public variables in different scripts? I am not sure if i understand your question;
But i am sure that what you want to achieve is that the object is moving downwards at certain speed; The Update function is called in every frame and the framerate is not constant, therefore your code will cause the object to move in framerate affected speed. To fix that, multiply your speed values by Time.deltaTime .
transform.position.y = transform.position.y - 1 * Time.deltaTime;
This will move your object one unit per second, no matter what's the framerate Same for the rotation
Oh hell, I forgot to change it back to show the bug I have. I've changed it to what it originally was so you can have a proper look at it. $$anonymous$$y bad? :S
But what I want is this:
Object falls at certain speed
When object is destroyed (used in another script) it changes the Speed variable.
So far it just sits there when I run it with the buggy script.
"When object is destroyed (used in another script)" ? You really have to be more clear
Sorry. I've included code in another script that destroys the object when it hits a specific object, but that sort of thing is getting kinda irrelevant. I think the only thing we need to focus on is just line 5.
Try a search on "unity finding var in another script." Some people use statics (which I$$anonymous$$HO always needs to be ripped out later.) The GetComponent method works for more things.
I'd also maybe not use "Start" for the other script name. Seems it might get confused with the Start function.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103006im_/https://answers.unity.com/themes/thub/images/avi.jpg)