Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

variable is not assigned a value

Discussion in 'Scripting' started by Aparty_, Dec 24, 2020.

Thread Status:
Not open for further replies.
  1. Aparty_

    Aparty_

    Joined:
    Apr 27, 2020
    Posts:
    41
    this method takes a floating point value and under certain conditions the value is assigned to a variable, but only inside the method and the global variable is not changed, there is another way to fix this

    Code (CSharp):
    1. class SimpleClass:MonoBehaviour{
    2. public float vertical=0.0f;
    3. public float horizontal=0.0f;
    4.  
    5. public void OnGo(float asd){if(asd>0.5f)horizontal=0.98f;Debug.Log(horizontal); if(asd>-0.5f)horizontal=-0.98f;Debug.Log(horizontal);}
    6.  
    7. public void OnBok(float bsd){if(bsd>0.5f)vertical=0.98f;Debug.Log(vertical); if(bsd>-0.5f)vertical=-0.98f;Debug.Log(vertical);}
    8.  
    9. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Can you please fix the formatting of your code? It's very difficult to read like this.

    Also, can you clarify your question? The wording of your post is confusing, I'm not really sure what you're asking.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Seriously. One thing per line please.

    There are no global variables. If you mean the public floats, beware that after you save an instance of this script, Unity's serialization will override any settings you put in field initializers (e.g, the 0.0f above).

    To avoid confusion, never initialize public variables in Unity.

    Instead, implement a
    void Reset()
    method and set default values there, which clearly shows that code must run to change values.
     
  4. Aparty_

    Aparty_

    Joined:
    Apr 27, 2020
    Posts:
    41
    two variables are constantly changing values very often.you can completely change the code, but functions and variables must beat
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Most likely because the variables are TERRIFIED of your long unreadable lines.

    Good luck!
     
  6. Aparty_

    Aparty_

    Joined:
    Apr 27, 2020
    Posts:
    41
    here is a readable code sorry for this inconvenience
    Code (CSharp):
    1. float v=0.0f;
    2. float h=0.0f;
    3. public void OnGo(float a)
    4. {
    5. h=a;
    6. }
    7. public void OnBok(float b)
    8. {
    9. v=b;
    10. }
    but it doesn't work.
     
    Last edited: Dec 28, 2020
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,968
    Duplicate / low effort.
     
Thread Status:
Not open for further replies.