Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# input issues

Discussion in 'Scripting' started by Mocha_Bear, Feb 19, 2015.

  1. Mocha_Bear

    Mocha_Bear

    Joined:
    Feb 19, 2015
    Posts:
    4
    Im very new to coding, having only taken a java course before this unity in C# course. I need to get a cube to rotate up and down with the keys, but currently the cube isnt repsonding to anything at all. Im sure this is a really simple fix but I cant seem to find it with my book or browsing other peoples questions/awnsers.
    also sorry I'm sure theres is a better way to add code to a post, I just couldnt seem to figure it out.

    using UnityEngine;
    using System.Collections;

    public class CubeRotator : MonoBehaviour {

    // Use this for initialization
    void Start () {




    }
    // Update is called once per frame
    void Update () {

    GameObject go;
    Transform got;

    go = GameObject.Find ("MyCube");
    got = go.transform;
    if (Input.GetKey(KeyCode.UpArrow))
    {
    print ("its going up");
    got.Rotate(new Vector3(25 * Time.deltaTime, 20,0));
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
    print ("its going down");
    got.Rotate (new Vector3 (-25 * Time.deltaTime, 20, 0));
    }

    }


    }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,935
    Does it print anything in console? Any error messages?
    Is the script attached to some object in the scene?

    There is "insert" button up here / on this message toolbar to insert code.
     
  3. Mocha_Bear

    Mocha_Bear

    Joined:
    Feb 19, 2015
    Posts:
    4
    Code (CSharp):
    1.     void Start () {
    2.  
    3.  
    4.    
    5.    
    6.         }
    7.     // Update is called once per frame
    8.         void Update () {
    9.  
    10.         GameObject go;
    11.         Transform got;
    12.  
    13.             go = GameObject.Find ("MyCube");
    14.             got = go.transform;
    15.         if (Input.GetKey(KeyCode.UpArrow))
    16.                 print ("its going up");
    17.                 got.Rotate(new Vector3(25 * Time.deltaTime, 20,0));
    18.                
    19.         if (Input.GetKey(KeyCode.DownArrow))
    20.                 print ("its going down");
    21.                 got.Rotate (new Vector3 (-25 * Time.deltaTime, 20, 0));
    22.            
    23.        
    24.         }
    25.    
    26.  
    27. }
    is the code then. and nope there is no error message and nothing prints out in the console. the box just sits there under the light.
     
  4. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    989
    I just tested this and it works just fine. Is the script attached to any gameobject in your scene and activated?
     
  5. Mocha_Bear

    Mocha_Bear

    Joined:
    Feb 19, 2015
    Posts:
    4
    the script is attached to a cube called MyCube.
     
  6. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    989
    Well in this case, I've no idea what's causing it to fail.

    if it's attached directly to the gameobject, you don't need to access it through your script. This could be your new script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeRotator : MonoBehaviour {
    5.  
    6. void Update () {
    7. if (Input.GetKey(KeyCode.UpArrow)) {
    8. print ("its going up");
    9. transform.Rotate(new Vector3(25 * Time.deltaTime, 20,0));
    10. }
    11. if (Input.GetKey(KeyCode.DownArrow)){
    12. print ("its going down");
    13. transform.Rotate (new Vector3 (-25 * Time.deltaTime, 20, 0));
    14. }
    15. }
    16. }
    have you modified time.timescale anywhere?
     
  7. Mocha_Bear

    Mocha_Bear

    Joined:
    Feb 19, 2015
    Posts:
    4
    no, havnted changed the time scale at all. youve seen my whole code.
     
  8. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Well, whatever it is, it is not this script that is causing the issue. Make sure you have attached the script to the cube, and have the cube active in the scene.

    Also try to set the 25 a bit lower, it might be PC issues preventing proper updating, and 25 * Time.deltaTime is super fast.

    Also stupid suggestion, but you are trying to press the arrow buttons right? Not the WSAD? (I had someone making that same mistake here some time ago).
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Script fixing 101, Debug.Log everything.

    Put a Debug.Log just inside the Update function, this will indicate if Update is being called.

    You have also missed braces on your if statements. This would mean the cube is moving up and down every frame. Same visual effect as a cube not moving.