How to print float items added inside a list
Hello everyone,
I'm totally new to coding and C# and I got stuck in a problem. I'm developing a test in Unity in which I need to save and print a list of reaction times. I have a cube who is moving towards the player. When the cubes enters an audiotrigger a sound is played. Then the player needs to press "S" and his reaction time is stored in a public List. What I am trying to do is to print all the elements of the list. At the bottom, I tried to use the foreach function but in this way the Console never stops to print the values in the list, while I need them to be printed only once.
At the moment I am trying to understand how to do this, but the final goal it would be to have something similar to this: // Debug.Log --> Reaction Time + index number of the list + = value of the reaction time OR 10 if RT > 2 I chose 10 to be an indicator of a "missed reaction time" which is important to me for further analysis.
May I ask you for your help?
Thank you very much
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ReactionTime : MonoBehaviour {
private bool isTiming = false;
private float timeTillKeyisPressed = 0;
public List <float> reactionTimes = new List<float>();
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("AudioTrigger"))
{
isTiming = true;
}
}
// Update is called once per frame
void Update ()
{
if (isTiming)
{
timeTillKeyisPressed += Time.deltaTime;
}
if (isTiming && Input.GetKeyDown(KeyCode.S) || timeTillKeyisPressed >= 2f)
{
reactionTimes.Add (timeTillKeyisPressed);
isTiming = false;
timeTillKeyisPressed = 0;
}
foreach (float item in reactionTimes)
{
Debug.Log (item);
}
}
}
forloop over foreach i think
EG
for(int i = 0; i < reactionTimes.Count; i++) { Debug.Log("Item value: " + reactionTimes[i]); }
Answer by unity_oVm0bd-LCrV4qQ · Feb 12, 2019 at 09:52 AM
Thank you for answering @zereda-games !
I've tried with your script and it works. The only thing is that for every new item in the list it prints me the whole list from the start, which I find quite redundant. But maybe it is something someone would prefer or that is more correct!
I've tried to write a condition to print the whole list (pretty redundant too). This writes me the whole list only once but only if it is completed, which might not be the best thing to do. Plus, I think there might be a smarter way to write the Debug.Logs, instead of writing 7 of them.
if (reactionTimes.Count == 7) {
Debug.Log ("ReactionTime1 = " + reactionTimes [0]);
Debug.Log ("ReactionTime2 = " + reactionTimes [1]);
Debug.Log ("ReactionTime3 = " + reactionTimes [2]);
Debug.Log ("ReactionTime4 = " + reactionTimes [3]);
Debug.Log ("ReactionTime5 = " + reactionTimes [4]);
Debug.Log ("ReactionTime6 = " + reactionTimes [5]);
Debug.Log ("ReactionTime7 = " + reactionTimes [6]);
}
These might be stupid details but I'm really interested in thinking "smart" while coding.
I hear yeah I'm Huge on being smart while coding lower case letters for private's and capital for public's and Use Attributes!!! ok i think i get what your mean now though, i had other posts but i removed them as they were not doing what you are asking. I'll try again and make it Smart :
SelectionBase=Grabs the actual game object with this component on it rather then it's parent if it is a child of another object.
Header(String$$anonymous$$essage)= Display a Bold separating lable into the inspector.
Tooltip(String$$anonymous$$essage) = Display's a tool tip in the inspector when hovering mouse over
Range($$anonymous$$Value,$$anonymous$$axValue)=Sets a max and $$anonymous$$ value for the slider;
SerializeField=$$anonymous$$akes a private variables visible in the inspector.
Space=As you would expect adds a small space between things in the inspector
Context$$anonymous$$enu(stringLable)=Add a new button to the gear list and is used by methods not variables
Context$$anonymous$$enuItem(stringLable, String$$anonymous$$ethod)=Add a right click feature to a variable.
ColorUsage=Control how you want the color to view in inspector.
For $$anonymous$$ore Info=https://unity3d.college/2017/05/22/unity-attributes/
Broke it down into 2 parts as i have limited Text space.
using System.Collections; Don't need
using System.Collections.Generic;
using UnityEngine;
[SelectionBase]
public class ReactionTime : $$anonymous$$onoBehaviour {
[Header("ReactionTime.cs :")]
[Space]
[Header("Variables Controlled@ Runtime (Viewable Only) :")]
*/Ran out of Text so I removed all summarys but this one. These are used to read when calling in other scripts, i usually make them the same as the Tooltip/*
/// <summary>
/// The Reaction-Times list that is created At Runtime when a player hits a $$anonymous$$ey
/// </summary>
[Tooltip("The Reaction-Times list that is created At Runtime when a player hits a $$anonymous$$ey")]
[Range(0.0f, 10.0f)]
[SerializeField]
private List <float> reactionTimes = new List<float>();
[Space]
[Tooltip(" Calculates how long it took the player to react.")]
[SerializeField]
[Range(0.0f, 10.0f)]
private float timeTill$$anonymous$$eyisPressed = 0;
[Space]
[Tooltip("What Selection Are we on? A$$anonymous$$A how many reaction has the player imputed?")]
[SerializeField]
[Range(0, 999)]
private static int selection=0;//NEW VARIABLE
[Space]
[Tooltip("Are we ti$$anonymous$$g the player's reaction?")]
[SerializeField]
private bool isTi$$anonymous$$g = false;
[Tooltip(" Has the 'Value' been logged to the console already?")]
[SerializeField]
private static bool hasLogged=false;//NEW VARIABLE
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("AudioTrigger"))
{
isTi$$anonymous$$g = true;
}
}
void Update ()
{
if (isTi$$anonymous$$g)
{
timeTill$$anonymous$$eyisPressed += Time.deltaTime;
}
//I'd give more time or else nothing will happen after 2 seconds.
if (isTi$$anonymous$$g && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S) || timeTill$$anonymous$$eyisPressed >= 10f)
{
reactionTimes.Add (timeTill$$anonymous$$eyisPressed);
//Giving you the 'Last Selection'
selection=reactionTimes.Count;//NEW
isTi$$anonymous$$g = false;
hasLogged=false//NEW
timeTill$$anonymous$$eyisPressed = 0;
}
//I'd do this :
//
for(int i=0;i< reactionTimes.Count;i++){
if(reactionTimes.Contains(timeTill$$anonymous$$eyisPressed)&&Equals(i,selection){
if(!hasLogged){
Debug.Log ("Reaction Time: "+reactionTimes[selection]);
hasLogged=true;
}
}
}
}
}
Hope this helps!
Add To Script if you like
If You would like to grab a specific selection in the array manually via a button in the scene do this and add it to an OnCallButton in unity.
public void GrabSelectionFromInputList(int selectionToGrab){
for(int i=0;i< reactionTimes.Count;i++){
if(reactionTimes.Contains(timeTill$$anonymous$$eyisPressed)&&Equals(i,selectionToGrab){
Debug.Log ("Reaction Time: "+reactionTimes[selectionToGrab]);
}
}
}