error in code get_deltatime

hi, i am new and i am learning, but i have a error and idk why this is the error:

get_deltaTime is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘Barra’ on game object ‘barra’.
See “Script Serialization” page in the Unity Manual for further details.
UnityEngine.Time:get_deltaTime()
Barra:.ctor() (at Assets/C#/Barra.cs:10)

this is my code

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

public class Barra : MonoBehaviour {

public float velocidad = 0.4f;
public float maximo = -3.75f;
public float minimo = 3.82f;
public float delta = Time.deltaTime;

void Start()
{

}

void Awake()
{
}
// Update is called once per frame
void Update()
{
float tecladohorizontal = Input.GetAxisRaw(“Horizontal”);
float posicionX = transform.position.x + (tecladohorizontal * velocidad * delta);
float negativo = (maximo * -1);
transform.position = new Vector3(Mathf.Clamp(posicionX, negativo, minimo), transform.position.y, transform.position.z);
}
}

and i dont know if i have to dow something in awake .-.

USE CODE TAGS!

And lastly it’s because you call ‘Time.deltaTime’ as an initializer for the field ‘delta’.

Field initializers are ran with the constructor.

Unity ‘constructs’ the script objects in a threaded serialization/deserialization process. They don’t like you accessing the unity API at that time, and rather you access it in the ‘Awake’ and ‘Start’ methods.

Don’t call Time.deltaTime as an initializer.

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

public class Barra : MonoBehaviour {

    public float velocidad = 0.4f;
    public float maximo = -3.75f;
    public float minimo = 3.82f;
    public float delta = Time.deltaTime;

    void Start()
    {

    }

     void Awake()
    {
    }
    // Update is called once per frame
    void Update()
    {
        float tecladohorizontal = Input.GetAxisRaw("Horizontal");
        float posicionX = transform.position.x + (tecladohorizontal * velocidad * delta);
        float negativo = (maximo * -1);
        transform.position = new Vector3(Mathf.Clamp(posicionX, negativo, minimo), transform.position.y, transform.position.z);
    }
}

Reply to my comment so you can see how I put your code in a block.

1 Like

There’s the problem.

You can’t use “delta” as a name. It’s already taken.

Also, you should declare tecladohorizontal, posicionX, and negativo outside of the update. Only use update to, well, update.

Also, also, update is for graphics. You appear to be making movements, which should be done on the physics loop. Instead of “Update” use “FixedUpdate”. And instead of Time.deltaTime use Time.fixedDeltaTime.

EDIT: Oh, and in the future make sure to post what the error said. Those errors can help point people to the problem. You, yourself, will want to start learning to use them to track down issues.

Nope, that’s not the problem. ‘delta’ is not already taken.

Why? They aren’t needed outside of that scope…

I know I’m not the greatest coder and you probably know more than me so all due respect there, BUT, the hell do you mean it’s not taken?

You said:

‘delta’ is not a reserved keyword, there’s no members of MonoBehaviour that already implements the name ‘delta’, it’s not taken in any way.

It’s perfectly safe to create a variable/field named ‘delta’ on a new script.

The problem is that you can’t initialize a variable/field with ‘Time.deltaTime’, since field initializers run during the constructor, and unity doesn’t let you access the unity api during a constructor.

1 Like

He’s re-declaring them every frame. That’s wasted CPU.

If that’s true, then why does it show up as used in the code block in my post up there?

OK… I could argue that making them fields is ‘wasted memory’.

It’s 3 floats on the stack… the cpu cycles lost are negligible.

Unity forums ‘guess’ hyperlinks based on names in your post. If you click the word, it’ll bring you to a search page of possible entries in the API ‘delta’ may refer to.

This does not necessarily mean you can’t use it as a field name though. It just means that there are other types that happen to share that name… but just because ‘Vector3’ has a function called ‘lerp’, doesn’t mean that the class Mathf can’t also have a method called ‘lerp’.

1 Like

Well alright then.

Thank you for the info.

so the soluition xdd? ?

PachiGG, just move Time.deltaTime to Update;

void Update()
{
   float tecladohorizontal = Input.GetAxisRaw("Horizontal");
   float posicionX = transform.position.x + (tecladohorizontal * velocidad * Time.deltaTime);
   float negativo = (maximo * -1f);
   transform.position = new Vector3(Mathf.Clamp(posicionX, negativo, minimo), transform.position.y, transform.position.z);
}

if i move delta i cant change it because i cant do a public class there

float posicionX = transform.position.x + (tecladohorizontal * velocidad * Time.deltaTime); not a public class :frowning:

i want to have delta in a public class

Time.deltaTime is a measure of how much time has passed between frames.

It’s a value type so it can’t be changed directly. You can however do this with its fixed value;

public float DeltaTime;

void Awake ()
{
  DeltaTime = Time.fixedDeltaTime;
}

…Wow this thread is a mess.

What @PachiGG has needed from the very beginning of this thread is an explanation of what Time.deltaTime is and how it’s used.

Time.deltaTime is the time between one frame and the next. It changes every single frame. Because it changes every frame, doing anything with it in initialization (your original code), Awake(), or Start() is absolutely pointless (with extremely rare exceptions). Just access Time.deltaTime directly, when you need it; don’t bother assigning it to anything.

The correct answer was posted by @grizzly a few posts ago, but then this bizarre reply

…and then the bad advice to use .fixedDeltaTime came. .fixedDeltaTime is not the right answer to that post, the right answer to that post is what does that even mean? “You want to have delta in a public class”? Why? What are you trying to accomplish? What do you mean, “not a public class”?

1 Like

ty

i want the deltatime in a public for change it to the right value for me, pls dont be rude :slight_smile:

Sorry, but I was being rude to the other people in this thread, who have been giving you bad advice (and, therefore, deserve a little rudeness, IMHO). Including grizzly’s last comment - that will not do what you want to do. It’ll compile and run, but Time.fixedDeltaTime is the wrong value to use in Update().

The point of Time.deltaTime is that it changes with your framerate. If you have a frame that takes longer, the Time.deltaTime will be larger that frame. So if your computer runs a game at 30fps and mine runs the game at 60fps, on your computer the character will move twice as far each frame, and will end up moving the same amount per second.

However, you can’t do that if you store the value of Time.deltaTime, or Time.fixedDeltaTime. If you use a stored value, then on my computer, the character will run twice as fast as on your computer, because he is using the same movement per frame, but I’m getting more frames. Understand?

If you use Time.deltaTime in Update, it will adjust to the correct value, and that’s all you need to do. It will change to the right value for you from one frame to the next.

If by “change to the right value for you” you mean you want to be able to make it larger or smaller in the Inspector, then you need to make a second number and multiply that by Time.deltaTime.

public float multiplier = 2f;
void Update() {
// ......
        float posicionX = transform.position.x + (tecladohorizontal * velocidad * multiplier * Time.deltaTime);