Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by unity_oVm0bd-LCrV4qQ · Feb 11, 2019 at 11:03 AM · listtime.deltatimeforeachdebug.logprint

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); 
         }
                     
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zereda-games · Feb 11, 2019 at 08:17 PM 0
Share

forloop over foreach i think

EG

for(int i = 0; i < reactionTimes.Count; i++) { Debug.Log("Item value: " + reactionTimes[i]); }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zereda-games · Feb 16, 2019 at 01:04 AM 0
Share

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/

avatar image zereda-games · Feb 16, 2019 at 01:04 AM 0
Share

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!

avatar image zereda-games zereda-games · Feb 16, 2019 at 01:29 AM 0
Share

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]);    
          }
      } 
 }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"InvalidOperationException Collection was modified" in a foreach loop 0 Answers

Get object to check list for number of specific items 0 Answers

Total war Style Movement System with Right Click 0 Answers

How to call all instances of a script in a list 0 Answers

How to run a "foreach" through a list of voids 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges