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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Script not running

Discussion in 'Scripting' started by lfawzan, Aug 24, 2021.

  1. lfawzan

    lfawzan

    Joined:
    Aug 24, 2021
    Posts:
    4
    Hello

    I just started learning Unity, and I was trying to create an object that moves. I added the following script to the object, but when I hit play nothing happens:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MovementScript : MonoBehaviour
    6. {
    7.     public float speed = 5f;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         float h = Input.GetAxis("Horizontal");
    18.         float v = Input.GetAxis("Vertical");
    19.  
    20.         Vector2 pos = transform.position;
    21.  
    22.         pos.x += h * speed * Time.deltaTime;
    23.         pos.y += v * speed * Time.deltaTime;
    24.  
    25.         transform.position = pos;
    26.     }
    27. }
    I'm using Unity 2020.3.13f1 for Mac.
    Would truly appreciate any help.
    Thanks a lot.
     
  2. DevGulpi

    DevGulpi

    Joined:
    Aug 6, 2021
    Posts:
    17
    I checked exactly same code with yours and it works
    Isn't the value (h or v) * speed * Time.deltaTime is too small to your game object looks moving?
     
  3. lfawzan

    lfawzan

    Joined:
    Aug 24, 2021
    Posts:
    4
    Thanks for the response chitPPO. I'm following a YouTube tutorial and it seems to be working for them. The object is visibly moving around. I tried to increase the speed however, but it still didn't work.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    First thing to do is add a Debug.Log statement to your Update method and see if it shows up in console. Don't be afraid to add several and print out values such as position or the value of v or h.

    This would be the first step to finding out if your code is even running.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  6. lfawzan

    lfawzan

    Joined:
    Aug 24, 2021
    Posts:
    4
    Thanks a lot for the great feedback!
    Ok so the script is actually running, but the problem is that my h and v variables have a value of 0 all the time.

    Code (CSharp):
    1. float h = Input.GetAxis("Horizontal");
    2. float v = Input.GetAxis("Vertical");
    This is why the character is not moving. Any ideas why this is happening?
    Thanks again.
     
  7. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    So when you hit Left/Right keys, the values do not change? Check your Key Bindings in Player Settings, i believe.
     
    Kurt-Dekker likes this.
  8. lfawzan

    lfawzan

    Joined:
    Aug 24, 2021
    Posts:
    4
    I'm sorry, the tutorial I'm following mentioned nothing about having to input anything, and their character seemed to be moving in a perfectly synchronous manner, so I thought the script was supposed to put the character into motion without me hitting anything. I looked up the input.getaxis command now and realized that I had no problem all along.

    Sorry to have wasted your time. Thanks for the help everyone!
     
    Last edited: Aug 26, 2021