• 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 /
  • Help Room /
avatar image
Question by ZeyvGaming · Jan 06, 2017 at 09:54 AM · scripting problemtutorialvoid

Value set in function doesn't update the glabal value

So I'm making the space shooter tutorial game and I was mockin around a bit to see if I could create a system where the asteroids would coma faster at you after a set amount of waves, however.. I'm now completely stuck on a piece of code that just doesn't want to work...

So I made a few variables that count down how many waves have appeared and when a certain limit has been reached I want it to update the speed which I did like so

 public class GameController : MonoBehaviour {
     // bunch of other variables
     public Mover spd 
     public int speedRounds
     private int speedCount
     
     IEnumerator SpawnWaves (){
             yield return new WaitForSeconds (startWait);
             while (true) {
                 if (speedCount < speedRounds) {
                     speedCount++;
                 }
                 if (speedCount == speedRounds) {
                     speedCount = 0;
                     spd.Faster ();
                 }
     
                 //code for making the asteroids spawn

following up with the following mover script where Faster(); is calling to

 public class Mover : MonoBehaviour {
     private Rigidbody rb;
     public float speed;
     public float maxSpeed;
 
     private float speedCalc;
     private float maxSpeedCalc;
     float speedMin;
     float maxSpeedMin;
 
     void Start () {
         Speed ();
         rb = GetComponent<Rigidbody> ();
         rb.velocity = transform.forward*speedCalc;
         //Debug.Log (speedCalc+ "," + speed+ ", " + speedMin);
     }
 
     public void Faster () {
         if (maxSpeedCalc > 0) {
             maxSpeedMin++;
             speedMin++;
             Speed ();
         } 
         else {
             return;
         }
     }
 
     void Speed (){
         maxSpeedCalc = maxSpeed - maxSpeedMin;
         speedCalc = speed - speedMin;
     }
 
     public void Reset (){
         speedMin = 0;
         maxSpeedMin = 0;
     }
 }

The problem is that, for some reason that I can't understand, it will update speedMin inside the void Faster () but nowhere else. Whenever I debug inside Faster () it will steadily count up the speedMin variable (which is also why I needed to put in the Reset () function which is called in MasterController's Start () so that they will reset when the game begins). However, when I debug anywhere else in the code it tells me that speedMin is still 0...

I'm pretty new to Unity (hence the tutorial) but I have some basic programming knowledge so I normally have a pretty good idea of what I can and cannot do but this just baffles me..

If anyone can help it would be most appreciated!

Comment

People who like this

0 Show 1
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 hexagonius · Jan 06, 2017 at 08:31 PM 0
Share

then either Reset is constantly called, you debug the values on different instances of the script then the one the increase is performed on, or the value is reset somewhere else not shown

0 Replies

· Add your reply
  • Sort: 

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

110 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d roguelike tutorial board manager issues 2 Answers

[HELP] Following the 2D character controller tutorial from the live training section (C#) 0 Answers

Surfing a like game 0 Answers

C# | What void do I put in if I want to do something once each click? 2 Answers

Code to replace the count commands in C# for text. 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