Search Unity

Help with a simple question (function update() seems to be only happening once)

Discussion in 'Scripting' started by akiel123, Apr 23, 2014.

  1. akiel123

    akiel123

    Joined:
    Nov 18, 2013
    Posts:
    7
    Okay, so I'm new to programming with unity, and I thought (honestly still think) that the "function Update()" ran every frame?
    I've got the following code:

    function Update () {
    print("wow");
    if(Input.GetButtonDown("Fire1")){
    transform.position = new Vector3(1,1,1);
    print("hey2");
    }if(Input.GetButtonDown("Fire2")){
    transform.position = new Vector3(-1,-1,-1);
    print("hey");
    }
    }

    and it only prints "wow" once to the console and doesn't move the object. Why not?

    EDIT: Actually I just found out that it gave me an print error 727 times, so I guess it keeps on doing it. The error was:
    "wow
    UnityEngine.MonoBehaviour:print(Object)
    Movement:Update() (at Assets/Movement.js:8)"

    How do I fix it? I'm guessing you shouldn't actually use print()? Still, the question of why it is not moving is most important :)

    EDIT 2: I've isolated the problem to being my if statement. If I remove it, the block will move, but even if I say
    if(Input.anyKeyDown)
    and press all over my keyboard it doesn't move.
     
    Last edited: Apr 23, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Are you sure its an error? print() works fine, and what you have posted isnt an error, its just what it looks like when you print something.

    And to your 'problem': GetButtonDown only fires on the single frame that you click, so I'd say thats 'working as intended'.
    And when you press the button, it will move the object immediatly to the position, if you alternate between Fire and Fire2 you should see it switching between the 2 posisiotns
     
  3. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229