There is a code from which I had to transfer several variables due to the fact that they are in the Update method called every frame, and they were reset to their original value, so that they would not be done. Is logical.

 using UnityEngine; using System.Collections; public class controller : MonoBehaviour { public void Start () { int thrust = 0; //changeable bool boost = false; float speed = 0f; //m/s } public static void Update () { // variable declaration int maxspd = 55; //} int boosted = 90; //} m/s^2 float accel = 13f;//} int boostacc = 16;//} Vector3 SPD; Vector3 TRANSLUP = new Vector3 (0, 0, -1); Vector3 TRANSLRIGHT = new Vector3 (0, 1, 0); Vector3 ROTUP = new Vector3 (0, 1, 0); Vector3 ROTAXIS = new Vector3 (1, 0, 0); // thrust changing and boost if(Input.GetAxis ("Mouse ScrollWheel") < 0){ Debug.Log(thrust + ", SCR U"); if(thrust < 10){ Debug.Log(thrust + ", UP"); thrust++; } }if(Input.GetAxis ("Mouse ScrollWheel") > 0){ Debug.Log(thrust + ", SCR D"); if(thrust > 0){ Debug.Log(thrust + ", DOWN"); thrust--; } } if(Input.GetMouseButton(2)){ // 2 = MMB boost = true; }else{ boost = false; } // acceleration and physics if(thrust != 0 && speed < maxspd){ speed += accel * Time.deltaTime; SPD = new Vector3 (-speed, 0, 0); transform.Translate(SPD); } if(thrust == 0 && speed > 0 && boost == false){ //assume braking speed += 20 * Time.deltaTime; SPD = new Vector3 (speed, 0, 0); transform.Translate(SPD); } // translation if(Input.GetKey("w")){ transform.Translate(TRANSLUP); } if(Input.GetKey("a")){ transform.Translate(-TRANSLRIGHT); } if(Input.GetKey("s")){ transform.Translate(-TRANSLUP); } if(Input.GetKey("d")){ transform.Translate(TRANSLRIGHT); } // controls rotation if(Input.GetKey("e")){ transform.Rotate(ROTAXIS); } if(Input.GetKey("q")){ transform.Rotate(-ROTAXIS); } if(Input.GetKey("f")){ transform.Rotate(ROTUP); } if(Input.GetKey("c")){ transform.Rotate(-ROTUP); } } } 

And here is the problem: I cannot create a variable in the Update method for the above reasons, but I cannot create them in the Start method, because then using them in the Update method will become impossible. Wildly I apologize for inaccuracies and such stupid questions, and it is possible that this is a duplicate.

    1 answer 1

    Let's start by saying that your class is a controller , and Start and Update are methods .

    To share variables in several methods of the same class, you can, for example, declare them as class fields . If the methods are static, then the fields should be static, and if the methods are non-static, then the fields should be like that.

    (Yes, non-static methods can apply to static fields, but I think that in your case, you need to make both Start and Update methods either static or non-static.)

    We get something like this:

     int thrust = 0; // общая переменная public void Start () { thrust = 1000; bool boost = false; float speed = 0f; //m/s } public void Update () { // не static if (thrust > 500) ... 
    • Replaced "classes" with "methods." Does this method allow only reading variables or reading and writing data in them? - JetFly
    • @JetFly: And record, of course, too. - VladD
    • Thanks for helping noob. - JetFly
    • one
      @JetFly: Please! It's not a shame to be a noob, everyone was like that. It is a shame not to ask questions and not to learn new things. So you are doing everything right. - VladD
    • It seems to me, or it’s wrong that I asked more questions than I gave answers. Maybe I’m wrong, but I think so. Thanks again. By the way, I realized that I clearly need the values ​​for acceleration and speed less. Shift by 56 units / frame somehow a lot. - JetFly