- Home /
Speed updated from another script, but doesn't persist
I have two scripts, one the "PlaneController" which is attached to the airplane you control, the other a "TargetTrigger" which is attached to each segment of a level you fly through.
The PlaneController has a variable called speed
which I want to be increased every time you fly through one of the triggers. The TargetTrigger script looks like this:
function OnTriggerEnter(other : Collider) {
if(other.gameObject.name == "Plane") {
Debug.Log("Calling Level Generator");
GameObject.Find("Generator").GetComponent(LevelGenerator).NewTunnelSection();
if(other.gameObject.GetComponent(PlaneSwiftController) != null) {
Debug.LogWarning("Speed up the plane from "+other.gameObject.GetComponent(PlaneSwiftController).speed+" to "+(other.gameObject.GetComponent(PlaneSwiftController).speed+50));
other.gameObject.GetComponent(PlaneSwiftController).speed += 50;
Debug.LogWarning("Speed is now "+other.gameObject.GetComponent(PlaneSwiftController).speed);
}
}
}
The PlaneController only ever sets the speed in the declaration and only ever uses it to push the airplane forward. Literally these are the only two places it is used:
public var speed : float = 200.0; // FIXED in swift mode (other values being kept in case we modify this)
// unrelated code...
function Update() {
Debug.LogWarning("Speed: "+this.speed);
// unrelated code...
rigidbody.velocity = transform.forward * speed * Time.deltaTime;
// unrelated code...
}
The log output is where it gets curious: sometimes the speed still reads as 200, despite the trigger being hit and indicating the speed was indeed increased. Here is an example output:
Speed up the plane from 200 to 250
Speed is now 250
Speed: 250
Speed: 200
Speed: 250
Speed: 200
Speed: 250
Speed: 200
Speed: 250
Speed: 200
Speed up the plane from 250 to 300
Speed is now 300
Speed: 300
Speed: 200
Speed: 300
Speed: 200
Notice that it alternates and it does so with the original speed. I wonder if this is an issue of a second thread of the PlaneController being started in the TargetTrigger script, but I don't see how since each call would then have to create a new thread, and the log doesn't support that.
Any clue what's going on?
Thanks!
public GameObject gameObject ; // In which attaching firstScript;
private firstScriptName firstScript;
function Start()
{
firstScript = gameObject.GetComponent<firstScriptName>();
}
function Update()
{
var secondVariable = firstScript.variableName ;
}
Try this idea
I'm afraid I don't follow. This script is referencing a second script - how does that apply to my example code?
Have you developed a script that decreased the speed? If yes, look for it in the objects of the scene. You may have it somewhere and then you trigger the action. If not then...it is somewhere else.
No, nothing decreases the speed. That wouldn't make much sense, anyway, since (as the speed log indicates) the speed is alternating between the increasing value (always correct) and the constant, originating value (always wrong).
Thank you! Feel free to submit that comment as an answer so I can accept it!
The plane had 3 controllers on it originally, since I was testing different ones. I recently added a new GameController which assigned the specific controller to the plane and, in the process, doubled-up. Oops!
The plane actually went a lot SLOWER with both scripts, because it was updating the speed of the first script, but only moving according to the second script. Not sure why (it does seem like it would make sense for it to double-up), but those were my results.
Thanks again fafase!
Answer by fafase · Nov 12, 2013 at 08:51 PM
Ok do you have that script twice on the object? GetComponent returns the first matching component. If you have it twice, only one is taken care.
Once you removed the second one, you will also see a drastic loss of speed in your object since he was moved twice each frame.