- Home /
How to pass a variable from one script to another correctly.
I am having problems passing a variable from one script to another. I thought I have followed the support documentation and what I can find on here to the letter but obviously not. I know what I am missing out is probably obvious and simple but can not for the life of me see it.
This script is supposed to pass the RespY value and is attached to an empty object. The idea being that this script can be altered to speed up or slow down the respiration rate and also the depth of breathing:
var TimeToNextBreathe: float = 4; var RespirationDuration: float = 3; var RespirationMaxAmplitude: float = 10; static var RespY: float; private var startTime : float;
function Start () {
startTime = Time.time;
} function Update () {
RespY = Mathfx.Hermite(0, 10, (Time.time - startTime) / RespirationDuration);
yield WaitForSeconds(TimeToNextBreathe - RespirationDuration);
}
This script then takes that value and plots the Y value - the x value is constant as it is a display like an ECG. The object this is attached to is a pre-fab trailrender hence the destruction when it jumps back to the start of the simulated monitor display. This may seems a complicated way of doing it but it is the only way I can see when my next step is to form heart waves based upon their composite parts. (yes Unity Trauma Centre is on the way).
var RespirationTrace: Transform; private var sX: float = 0; private var sY: float = 0;
function Start() {
yield WaitForSeconds (30);
Destroy (gameObject);
}
function Breathe() {
sY = RespirationController.RespY;
transform.position = Vector3(sX, sY, 0);
sX = sX + (20 * Time.deltaTime);
}
Any help would be gratefully received and highly appreciated.
Answer by Lipis · Mar 05, 2010 at 10:45 PM
Your first script is more or less doing nothing because start()
and update()
are not being called by Unity since the function names are wrong. Try changing the names of the functions from start()
to Start()
and update()
to Update()
. You also have to remove this line from the Update()
, because it can't be a coroutine:
yield WaitForSeconds(TimeToNextBreathe - RespirationDuration);
Edit: Here is an example on how you can achieve a delay every 2 seconds:
private var timer: float = 0.0; private var breathing: boolean = false;
function Start() { timer = 0.0; breathing = true; }
function Update () { if (breathing) { timer += Time.deltaTime; }
if (timer >= 2) {
//Do something useful every 2 seconds (like take a breath)
timer = 0.0;
}
}
Truly ashamed now that I looked at all the complicated stuff and missed the obvious. Thank you just need to shift the coroutine out of the update.
@$$anonymous$$de don't be ashamed of asking any question we all did these kind of mistakes in the beginning.
Thank you for you kind words. I have made the changes you said and also attempted to put the 2nd script part as a coroutine as it seems to indicate me to in the manual (the debugger said I can not have a coroutine in an update). The value is still not passed over though as I have a straight horizontal line.
@$$anonymous$$de Try removing yield from Update(), this is what the debugger trying to tell you.
Yes that works perfectly waveform wise :-) How do I then use that delay I had implemented that the next breathe is not necessarily taken immediately.
Your answer
Follow this Question
Related Questions
Variable having different value in different methods / carrying over value from last play 1 Answer
Are there restrictions on assigning GameObjects to Prefab script variables? 3 Answers
Problem setting up an array with a size determined by a variable 2 Answers
how to assign a variable to Debug.Log() ? 1 Answer
Animation.Play(useVar) 0 Answers