user13074005
user13074005

Reputation:

Unity not running basic script

I am a first time programmer watching a tutorial on making a 2d, top-down game. The tutorial is slightly outdated but only by a couple years, so I assume that it would work the same. My goal for this specific section of code is just to make the player look left when A is pressed and look right when D is pressed. Right now, nothing happens, but I don't see any errors in the console.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player : MonoBehaviour
{
    private BoxCollider2D boxCollider;

    private Vector3 moveDelta;

    private void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
    }

    private void FixedUpdated()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        //Reset moveDelta
        moveDelta = new Vector3(x, y, 0);

        //Swap sprite direction for left or right
        if (moveDelta.x > 0)
            transform.localScale = Vector3.one;
        else if (moveDelta.x < 0)
            transform.localScale = new Vector3(-1, 1, 1);
    }
}

Do I have a setting on Unity wrong, or is there just an error in my code?

The tutorial I'm following is "Learn Unity Engine and C# by creating a real top down RPG" on Udemy and I don't see anyone else having the same problem.

Any help is appreciated :)

Upvotes: 1

Views: 726

Answers (1)

Pac0
Pac0

Reputation: 23174

Unity3D works with some functions that have to match exactly some predefined names.

You wrote "FixedUpdated", that's not a function that Unity knows about, so it will never call it "automagically".

You probably have mistaken it for FixedUpdate, which is a function called every frame by Unity.

Note that, unfortunately, the compiler will never raise an error for that.

However, there will be a message about this in Visual Studio. I strongly recommend to check the tab "Errors", by default in the bottom, and un-filter "information" messages, and read them. :

IDE0051   Private member 'NewBehaviourScript.FixedUpdated' is unused

Visual Studio 2019 showing message that FixedUpdated is unused

Upvotes: 1

Related Questions