Search Unity

My Script Sometimes Works and Sometimes Doesn't Works

Discussion in 'Scripting' started by NotAlwaysCoding, Dec 18, 2019.

  1. NotAlwaysCoding

    NotAlwaysCoding

    Joined:
    Sep 1, 2018
    Posts:
    11
    Hello, I'm new in programming and I don't want to end hating it, I'd like to know why my script sometimes works and sometimes it doesn't works when I press Play in the Unity Editor, I've tried changing it from a chain of "Ifs" to a Switch case now and still the same problem.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. //using EZCameraShake;
    5. public class TimerIncrease : MonoBehaviour
    6. {
    7.     public float timeStart;
    8.     public Text textBox;
    9.     public GameObject myObject, myObject2, myObject3, myObject4, myObject5, myObject6, myObject7, myObject8;
    10.     // Start is called before the first frame update
    11.    
    12.     void Awake()
    13.     {
    14.         textBox.text = timeStart.ToString("F2");
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         timeStart += Time.deltaTime;
    21.         textBox.text = timeStart.ToString("F2");
    22.  
    23.         //Activar Voz
    24.         //VozenOff();
    25.         //Aparecer Objetos
    26.         InvokeObj();
    27.         //Shake
    28.         //ToShake();
    29.     }
    30.  
    31.     private void InvokeObj()
    32.     {
    33.         switch(textBox.text)
    34.         {
    35.             case "5.00":
    36.             SoundManager.PlaySound("dialogouno");
    37.             break;
    38.  
    39.             case "10.00":
    40.             myObject.SetActive(true);
    41.             SoundManager.PlaySound("dialogodos");
    42.             break;
    43.  
    44.             case "12.00":
    45.             myObject2.SetActive(true);
    46.             myObject7.SetActive(true);
    47.             break;
    48.  
    49.             case "14.00":
    50.             myObject3.SetActive(true);
    51.             break;
    52.  
    53.             case "16.00":
    54.             myObject4.SetActive(true);
    55.             break;
    56.  
    57.             case "18.00":
    58.             myObject5.SetActive(true);
    59.             break;
    60.  
    61.             case "9.00":
    62.             myObject6.SetActive(true);
    63.             break;
    64.  
    65.             case "15.00":
    66.             SoundManager.PlaySound("dialogotres");
    67.             break;
    68.  
    69.             case "20.00":
    70.             SoundManager.PlaySound("dialogocuatro");
    71.             break;
    72.  
    73.             case "30.00":
    74.             SoundManager.PlaySound("dialogocinco");
    75.             break;
    76.  
    77.             case "40.00":
    78.             SoundManager.PlaySound("dialogoseis");
    79.             break;
    80.            
    81.             case "47.00":
    82.             SoundManager.PlaySound("dialogosiete");
    83.             break;
    84.         }
    85.     }
    86.     //CAMAR SHAKE SIN VR
    87.     /*private void ToShake(){
    88.  
    89.         if (textBox.text == "10.20")
    90.         {
    91.             CameraShaker.Instance.ShakeOnce(4f, 4f, .1f, 1f);
    92.         }
    93.         if (textBox.text == "12.20")
    94.         {
    95.             CameraShaker.Instance.ShakeOnce(4f,4f, .1f, 1f);
    96.         }
    97.         if (textBox.text == "14.20")
    98.         {
    99.             CameraShaker.Instance.ShakeOnce(4f,4f, .1f, 1f);
    100.         }
    101.         if (textBox.text == "16.20")
    102.         {
    103.             CameraShaker.Instance.ShakeOnce(4f,4f, .1f, 1f);
    104.         }
    105.         if (textBox.text == "18.50")
    106.         {
    107.             CameraShaker.Instance.ShakeOnce(8f,8f, .2f, 1f);
    108.            
    109.         }
    110.     }*/
    111. }
    112.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The goal here is to, I assume, play these sounds when the time reaches a given value?

    Consider this: what happens when a frame goes from 4.99 to 5.01? Hopefully this will make it obvious why it sometimes doesn't work.

    What you need to do is to make a bool containing whether a given event has been triggered, and then use timeStart > 5.0f, not a string comparison. With your current code structure, that IS going to mean creating a bool for every single event you can trigger. But there's a better way: animation.

    If you need to do a specific timed sequence of events, you're almost always better off making it an animation or a Timeline thing. You can animate any value (including public variables on your own scripts), and use that value to trigger events. You can also use animation events to trigger specific events at times. This is going to be much more robust, tweakable, and reliable than directly comparing to a given time value.
     
    NotAlwaysCoding and ZaffreSheep like this.
  3. ZaffreSheep

    ZaffreSheep

    Joined:
    Aug 26, 2018
    Posts:
    32
    Hey GiuseppeArtadi,
    Next time you get the chance, try using Debug.Log(Time.deltatime) each frame, and check out the console. I believe what's happening is that timeStart not hitting exactly on 5.00, 10.00, 12.00, etc. You may have to use Mathf.Floor(), to make sure this works.

    Edit: StarManta's suggestion about animation would work well too. There's always more than one way to figure somethin' out.
     
    NotAlwaysCoding likes this.
  4. NotAlwaysCoding

    NotAlwaysCoding

    Joined:
    Sep 1, 2018
    Posts:
    11
    I've tried the way of timeStart > 5.0f and changed my switch to if, but now what happens is that when it set active a gameobject or it plays a sound, the script does it many times, multiple times, infinite :( and the animation way I don't get it.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. //using EZCameraShake;
    5. public class TimerIncrease : MonoBehaviour
    6. {
    7.     public float timeStart;
    8.     public Text textBox;
    9.     public GameObject myObject, myObject2, myObject3, myObject4, myObject5, myObject6, myObject7, myObject8;
    10.     // Start is called before the first frame update
    11.    
    12.     void Awake()
    13.     {
    14.         textBox.text = timeStart.ToString("F2");
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         timeStart += Time.deltaTime;
    21.         textBox.text = timeStart.ToString("F2");
    22.  
    23.         //Activar Voz
    24.         VozenOff();
    25.         //Aparecer Objetos
    26.         InvokeObj();
    27.     }
    28.  
    29.     private void InvokeObj()
    30.     {
    31.         if (timeStart > 10.00f)
    32.         {
    33.             myObject.SetActive(true);
    34.             Debug.Log("GOTCHA");
    35.         }
    36.         if (timeStart > 12.00f)
    37.         {
    38.             myObject2.SetActive(true);
    39.         }
    40.         if (timeStart > 14.00f)
    41.         {
    42.             myObject3.SetActive(true);
    43.         }
    44.         if (timeStart > 16.00f)
    45.         {
    46.             myObject4.SetActive(true);
    47.         }
    48.         if (timeStart > 18.00f)
    49.         {
    50.             myObject5.SetActive(true);
    51.         }
    52.         if (timeStart > 9.00f)
    53.         {
    54.             myObject6.SetActive(true);
    55.         }
    56.         if (timeStart > 12.00f)
    57.         {
    58.             myObject7.SetActive(true);
    59.         }
    60.     }
    61.     private void VozenOff()
    62.     {
    63.         //Voz en off
    64.         if (timeStart > 5.00f)
    65.         {
    66.             SoundManager.PlaySound("dialogouno");
    67.             Debug.Log("SOUNDDDDDDDDDDDDD");
    68.         }
    69.         if (timeStart >  10.00f)
    70.         {
    71.             SoundManager.PlaySound("dialogodos");
    72.             Debug.Log("SOUNDDDDDDDDDDDDD");
    73.         }
    74.         if (timeStart >  15.00f)
    75.         {
    76.             SoundManager.PlaySound("dialogotres");
    77.             Debug.Log("SOUNDDDDDDDDDDDDD");
    78.         }
    79.         if (timeStart >  20.00f)
    80.         {
    81.             SoundManager.PlaySound("dialogocuatro");
    82.             Debug.Log("SOUNDDDDDDDDDDDDD");
    83.         }
    84.         if (timeStart >  30.00f)
    85.         {
    86.             SoundManager.PlaySound("dialogocinco");
    87.         }
    88.         if (timeStart > 40.00f)
    89.         {
    90.             SoundManager.PlaySound("dialogoseis");
    91.         }
    92.         if (timeStart >  47.00f)
    93.         {
    94.             SoundManager.PlaySound("dialogosiete");
    95.         }
    96.     }
    97.  
    98.  
    99.     //CAMAR SHAKE SIN VR
    100.     /*private void ToShake(){
    101.  
    102.         if (timeStart > "10.20")
    103.         {
    104.             CameraShaker.Instance.ShakeOnce(4f, 4f, .1f, 1f);
    105.         }
    106.         if (timeStart > "12.20")
    107.         {
    108.             CameraShaker.Instance.ShakeOnce(4f,4f, .1f, 1f);
    109.         }
    110.         if (timeStart > "14.20")
    111.         {
    112.             CameraShaker.Instance.ShakeOnce(4f,4f, .1f, 1f);
    113.         }
    114.         if (timeStart > "16.20")
    115.         {
    116.             CameraShaker.Instance.ShakeOnce(4f,4f, .1f, 1f);
    117.         }
    118.         if (timeStart > "18.50")
    119.         {
    120.             CameraShaker.Instance.ShakeOnce(8f,8f, .2f, 1f);
    121.            
    122.         }
    123.     }*/
    124. }
    125.  
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That's why you set up a bool:
    Code (csharp):
    1. bool thing1Triggered = false;
    2. void Update() {
    3. if (timeStart > 10f && !thing1Triggered) {
    4. Debug.Log("GOTCHA");
    5. myObject.SetActive(true);
    6. thing1Triggered = true;
    7. }
     
    NotAlwaysCoding and Joe-Censored like this.
  6. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528