Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to use SerializeField to declare how many frames in speed.

Discussion in 'Scripting' started by Dungeondisaster, Jul 25, 2021.

  1. Dungeondisaster

    Dungeondisaster

    Joined:
    Sep 11, 2020
    Posts:
    6
    For my game, I created a typewriter script that made each character come out one at a time, and I wanted to be able to customize the speed of each character from unity using a SerializeFeild, but I have no idea how to implement the variable into my code. Here it is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class Typewriter : MonoBehaviour
    8. {
    9.     [SerializeField] TextMeshProUGUI textComponent;
    10.     [SerializeField] float Speed;
    11.  
    12.  
    13.  
    14.  
    15.     string story;
    16.  
    17.     void Awake()
    18.     {
    19.      
    20.         story = textComponent.text;
    21.         textComponent.text = "";
    22.  
    23.      
    24.         StartCoroutine("PlayText");
    25.     }
    26.  
    27.     IEnumerator PlayText()
    28.     {
    29.         foreach (char c in story)
    30.         {
    31.             textComponent.text += c;
    32.             yield return new WaitForSeconds(Speed);
    33.              //Speed was previously shown as .125f
    34.              //how do you use an f next to a float?!?!
    35.              //WHY MUST I SUFFER AHHH
    36.         }
    37.     }
    38.  
    39. }
    You may notice my slow descent to insanity at the end of the code. Please help me!
     
    Last edited: Jul 25, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Not sure what the question is because apart from your descent into insanity, everything you're doing seems eminently reasonable. In the editor inspector window you would just type 0.125, you do NOT type the
    f
    in the editor.

    (I am also assuming that the Color and /Color stuff in the above code is related to a copy/paste error... none of that can be in the actual code in the game!)

    ALSO, it is more reliable to say this:

    Code (csharp):
    1. StartCoroutine(PlayText());
    Which eliminates the possibility that you misspell "PlayText" and lets the compiler catch it.
     
  3. Dungeondisaster

    Dungeondisaster

    Joined:
    Sep 11, 2020
    Posts:
    6
    Oh! That code for the color was my sad attempt of highlighting the Speed variables. I will try and edit that part of my post.
    My biggest problem is that when I do put in, say, 1 in for Speed back in the editor, Unity does not recognize this as a speed back in the code (by guess of why being due to the missing f), and of the text simply appears as if I had no script at all, unlike what my code originally did (when it was .125f), where it wrote out each character 1 at a time.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Let me offer my blurbs about the mechanism you might be seeing, a mechanism which may actually trigger feelings of descent into insanity when you first encounter it, but is completely defined and understandable:

    Field initializers versus using Reset() function and Unity serialization:

    https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908

    https://forum.unity.com/threads/crouch-speed-is-faster-than-movement-speed.1132054/#post-7274596
     
  5. Dungeondisaster

    Dungeondisaster

    Joined:
    Sep 11, 2020
    Posts:
    6
    Thank you! I think I understand what you are saying, though I am struggling with its implementation. I understand what you said in both posts, but by adding the reset I can no longer change the Speed value from within the editor, and the speed is always extraordinarily fast and the same.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class Typewriter : MonoBehaviour
    8. {
    9.     [SerializeField] TextMeshProUGUI textComponent;
    10.     public float Speed; //no more Serialize, just has been made public.
    11.  
    12.  
    13.     void Reset()
    14.     {
    15.         Speed = 1f;
    16.     }
    17.  
    18.     string story;
    19.  
    20.     void Awake()
    21.     {
    22.      
    23.         story = textComponent.text;
    24.         textComponent.text = "";
    25.  
    26.      
    27.         StartCoroutine(PlayText());
    28.     }
    29.  
    30.     IEnumerator PlayText()
    31.     {
    32.         foreach (char c in story)
    33.         {
    34.             textComponent.text += c;
    35.             yield return new WaitForSeconds(Speed);
    36.         }
    37.     }
    38.  
    39.     }
    40.  
    Do you know what my problem might be? Did I put the reset in the wrong position or rephrase what is inside it?
     
    Last edited: Jul 25, 2021