Search Unity

Unity C# Fundamentals Pluralsight

Discussion in 'Scripting' started by Sterile_D, Mar 4, 2019.

  1. Sterile_D

    Sterile_D

    Joined:
    Apr 6, 2015
    Posts:
    16
    I am working on the Unity Fundamentals C# videos. We are at the end of the "for loops" video. When I hit the space bar nothing happens in Unity. It is supposed to print 5 lines in the console depending on the random numbers it generates. Any help would be appreciated. Here is what the script looks like:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ExampleScript_B : MonoBehaviour
    7. {
    8. private int enemyDistance = 0;
    9. // Use this for initialization
    10. void Start()
    11. {
    12.  
    13. }
    14.  
    15. // Update is called once per frame
    16. void Update()
    17. {
    18. if (Input.GetKeyUp("space"))
    19. {
    20. EnemySearch();
    21. }
    22. }
    23.  
    24. void EnemySearch()
    25. {
    26. for (int i = 0; i < 5; i++)
    27. {
    28. enemyDistance = Random.Range(1, 10);
    29.  
    30. if (enemyDistance >= 8)
    31. {
    32. print("An enemy is far away!");
    33. }
    34. if (enemyDistance >= 4 && enemyDistance <= 7)
    35. {
    36. print("An enemy is at medium range!");
    37. }
    38. if (enemyDistance < 4)
    39. {
    40. print("An enemy is very close by!");
    41. }
    42. }
    43. }
    44. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Did you attach the script to a GameObject in the scene?
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Personally I seldom use the string parameter when using input to get keyboard inputs like 'space'. And I'm not at home right now to test this...

    but I'm fairly certain that the string parameter methods (even for GetKey***) use the names configured in the Game Input Manager... and the documentation seems to imply as such:
    https://docs.unity3d.com/ScriptReference/Input.GetKeyUp.html

    Is there an input named 'space'?

    Or why not use the version that accept KeyCode?

    Code (csharp):
    1. if (Input.GetKeyUp(KeyCode.Space))
    ...

    I do find irony that an article/video being created for a pay service is having to consult the free forums where we donate our time for what is a fundamental part of unity and relatively easy to test if you have the environment on hand (unfortunately I don't on this laptop).

    [edit]
    OH, you're following the video. I read this as you're creating some video.
     
    Last edited: Mar 4, 2019
  4. Sterile_D

    Sterile_D

    Joined:
    Apr 6, 2015
    Posts:
    16
    Thank you for the help. I believe it was due to not attaching the script.
     
  5. Deleted User

    Deleted User

    Guest