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 .-.
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.
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.
‘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.
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’.
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”?
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.