• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ende · Mar 05, 2010 at 10:29 PM · variable

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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;
 }

}

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ende · Mar 05, 2010 at 10:51 PM 0
Share

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.

avatar image Lipis · Mar 05, 2010 at 10:58 PM 0
Share

@$$anonymous$$de don't be ashamed of asking any question we all did these kind of mistakes in the beginning.

avatar image Ende · Mar 05, 2010 at 11:06 PM 0
Share

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.

avatar image Lipis · Mar 05, 2010 at 11:21 PM 0
Share

@$$anonymous$$de Try removing yield from Update(), this is what the debugger trying to tell you.

avatar image Ende · Mar 05, 2010 at 11:25 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Transfer values from one script to another 1 Answer

Change variable in Mono from other script 1 Answer

Random time interval 1 Answer

error modifying a var 1 Answer

Temperary Variables 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges