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

Literally copied code from a tutorial but it won't work !

Discussion in 'Getting Started' started by Evil_Chicken, Aug 9, 2022.

  1. Evil_Chicken

    Evil_Chicken

    Joined:
    Aug 9, 2022
    Posts:
    2
    I attach this code to an object but nothing happens.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MyFirstScript : MonoBehaviour
    6. {
    7.     public float speed = 5f;
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         float h = Input.GetAxis("Horizontal");
    16.         float v = Input.GetAxis("Vertical");
    17.         Vector2 pos = transform.position;
    18.         pos.x += h * speed * Time.deltaTime;
    19.         pos.y += v * speed * Time.deltaTime;
    20.         transform.position = pos;
    21.     }
    22. }
    23.  
    So I searched a bit, looking for these classes and methods but I can't find anything wrong.
    I thought maybe I'm not attaching it to the object properly or is it because of the script itself?
    So I tried going with something simpler and made a new script.
    Code (CSharp):
    1. public class moveDidi : MonoBehaviour
    2. {
    3.     public float speed=10f;
    4.     void Start()
    5.     {
    6.     }
    7.     void Update()
    8.     {
    9.         transform.position += transform.forward * speed * Time.deltaTime;
    10.     }
    11. }
    But again this also didn't work.
    So I tried again with another one.
    Code (CSharp):
    1. public class banana : MonoBehaviour
    2. {
    3.    
    4.     void Start()
    5.     {
    6.        
    7.     }
    8.  
    9.     void Update()
    10.     {
    11.         transform.position = new Vector2(0, 2) * Time.deltaTime;
    12.     }
    13. }
    And it worked. However it is the only one that works. What am I doing wrong here?
     
  2. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    I don't know if it's related to the issue of your code not working, but C# is VERY case sensitive, also watch your spelling, if you spell a word wrong your code definitely won't work.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Did you spot a misspelling? Please point it out, if so. Otherwise, I might suggest to the OP to place

    Debug.Log("I'm here in Start");

    in the Start() method to ensure the code is actually executing.
     
  4. Evil_Chicken

    Evil_Chicken

    Joined:
    Aug 9, 2022
    Posts:
    2
    I tried this and the code is being executed. I tried a couple other things to fix it, but none worked.
    Then I changed the code from
    Code (CSharp):
    1. public class moveDidi : MonoBehaviour
    2. {
    3.     public float speed=10f;
    4.     void Start()
    5.     {
    6.     }
    7.     void Update()
    8.     {
    9.         transform.position += transform.forward * speed * Time.deltaTime;
    10.     }
    11. }
    to
    Code (CSharp):
    1. public class moveDidi : MonoBehaviour
    2. {
    3.     public float speed=10f;
    4.     void Start()
    5.     {
    6.     }
    7.     void Update()
    8.     {
    9.         transform.position = transform.position + transform.forward * speed * Time.deltaTime;
    10.     }
    11. }
    And it worked. I don't know why this happens. So I managed to make this one work and I went back to first code from the tutorial.
    I tried using
     Debug.Log("H=" +h);
    in the update() method, and the result is always H=0.
    Then with a bit of googling, I realized that I need to press the arrow keys for the object to start moving, I was just sitting and waiting for the object to move by itself...
     
  5. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    I had asked to put a Debug.Log in Start, not Update, to confirm the code is executing. I was being specific. And H=0 is the correct output in your case. But I'm glad you resolved your issue.