Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 myingling · Nov 11, 2013 at 03:42 PM · variablespeedthreads

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!

Comment
Add comment · Show 5
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 LijuDeveloper · Nov 11, 2013 at 05:40 PM 0
Share
 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

avatar image myingling · Nov 12, 2013 at 08:36 PM 0
Share

I'm afraid I don't follow. This script is referencing a second script - how does that apply to my example code?

avatar image fafase · Nov 12, 2013 at 08:41 PM 0
Share

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.

avatar image myingling · Nov 12, 2013 at 08:43 PM 0
Share

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).

avatar image myingling · Nov 13, 2013 at 01:05 AM 0
Share

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!

1 Reply

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

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.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using variable from one script attached to one object in another script attached to another object 0 Answers

Create a variable of type C# script? 1 Answer

Float var to control animation speed? 1 Answer

How to call Variable across scripts. 2 Answers

Scripting error! 1 Answer


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