- Home /
Float sets itself to zero
I scrapped the excess out, but there is a really mind bogling problem in the code. Look at the void Start() I can't understand why private floats, that are set to 1.0f are zero?
using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using System; using System.Collections;
public class Minigame : MonoBehaviour {
 public CutoffTimer cutoff;
 public SprintConnection conn;
 public SprintPlayerData playerData;
 public Text minigameTimer;
 public Text minigameScore;
 public string timerText;
 public bool timeOver;
 private int tournamentId;
 private int minigameId;
 private int currentScore;
 private float interval1Sec;
 private float offlineTimeLeft;
 private float offlineTimeMax;
 private bool offline;
 private float onlineTimeLeft = 15;
 private float onlineTimeMax = 15;
 private bool onlineTimerSet;
 private float scaleFactor = 0.5f;
 private float accelerometer;
 private float gyroscope;
 int maxCheckCounter = 0;
 int maxCheckInterval = 6;
 **[System.NonSerialized]
 private float aK, bK, xK = 1.0f;
 void Start()
 {
     Debug.Log(bK + "," + aK); //<---- This returns 0,0 - even when above bK & aK are set to 1.0f?
     Input.gyro.enabled = true;
 }**
 public void updateScore(int score)
 {
     if (!timeOver)
     {
         currentScore = score;
         minigameScore.text = currentScore.ToString();
     }
 }
 public float GetAccelerometer(string xyz)
 {
     float acc = 4.0f / accelerometer;
     xyz = xyz.ToLower();
     if (xyz != "x" && xyz != "y" && xyz != "z")
     {
         return 0;
         Debug.Log("NOLLA?");
     }
     else
     {
         if (Application.platform == RuntimePlatform.WSAPlayerARM || Application.platform == RuntimePlatform.WSAPlayerX64 || Application.platform == RuntimePlatform.WSAPlayerX86)
         {
             switch (xyz)
             {
                 case "x":
                     return Math.Min(6.0f, Math.Max(-6.0f, Input.acceleration.x * 1.72f));
                     break;
                 case "y":
                     return Math.Min(6.0f, Math.Max(-6.0f, Input.acceleration.y * 1.7f));
                     break;
                 case "z":
                     return Math.Min(6.0f, Math.Max(-6.0f, Input.acceleration.z * 1.6f));
                     break;
                 default:
                     Debug.Log("NOLLA?");
                     return 0.0f;
                     break;
             }
         }
         else
         {
             switch (xyz)
             {
                 case "x":
                     return aK * Math.Min(4.0f, Math.Max(-4.0f, Input.acceleration.x * xK * acc));
                     break;
                 case "y":
                     return aK *  Math.Min(4.0f, Math.Max(-4.0f, Input.acceleration.y * bK * acc));
                     break;
                 case "z":
                     return aK * Math.Min(4.0f, Math.Max(-4.0f, Input.acceleration.z * bK * acc));
                     break;
                 default:
                     Debug.Log("NOLLA?");
                     return 0.0f;
                     break;
             }
         }
     };
 }
 public float GetGyro(string xyz)
 {
     float acc = 18 / gyroscope;
     xyz = xyz.ToLower();
     if (xyz != "x" && xyz != "y" && xyz != "z")
     {
         return 0;
     }
     else
     {
         switch (xyz)
         {
             case "x":
                 return Math.Min(4 * xK, Math.Max(-4 * xK, Input.gyro.rotationRate.x * acc));
                 break;
             case "y":
                 return Math.Min(4 * xK, Math.Max(-4 * xK, Input.gyro.rotationRate.y * acc));
                 break;
             case "z":
                 return Math.Min(4 * xK, Math.Max(-4 * xK, Input.gyro.rotationRate.z * acc));
                 break;
             default:
                 return 0;
                 break;
         }
     };
 }
}
Answer by hexagonius · Nov 01, 2016 at 10:33 AM
only xK is set to 1. Such one-liners define multiple variables at once, but each needs it's initial value when differing from 0.
Unfortunately there's no such syntax initializing all at once 
As a follow-up, it might help to get an Intro to C# program$$anonymous$$g book and skim over the first few chapters (that's straight program$$anonymous$$g, not even C#. I even have an example covering exactly this in $$anonymous$$e.) You'll probably find a bunch of other rules you didn't know, save you a lot of time.
Your answer
 
 
             Follow this Question
Related Questions
Why inpector keeps methods assigned when changed from public to private 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to fix wobbling float when away from spawn? 1 Answer
Float bug... no errors. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                