I cannot use it Input.GetAxis defined ias a variable with my horizontalInput ; and public float verticalInput ;variables
I get this error message
UnityException: GetAxis is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘Move’ on game object ‘player’.
See “Script Serialization” page in the Unity Manual for further details.
Move…ctor () (at Assets/scripts/Move.cs:11)
I have tried in my variables before void start below and commented it our in void update for verticalInput
It works when it is activated in Void Update
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public int speed = 1;
public float horizontalInput ;
//public float verticalInput ;
public float verticalInput = Input.GetAxis("Vertical");
// Start is called before the first frame update
void Start()
{
//transform.position = new Vector3(0, 4, 0);
}
// Update is called once per frame
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
//verticalInput = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * speed * Time.deltaTime);
// transform.Translate(Vector3.left * speed *Time.deltaTime);
}
}
All the “assignment code” (e.g. in line 11) in a class happens only once, when a class is created, and is never run again. Unity’s protecting you, because even if it did allow that code to run, it wouldn’t do anything. You need to retrieve the input values each frame for it to be valid… so do it in Update.
Yes, and that’s the correct place to do that. Why are you trying to do the other thing?
Debug.Log(“LA CASA Y EL GALLO”);
Debug.Log(“Comienza la aventura. Tienes 3 vidas. Suerte…”);
Debug.Log(“Estás frente a una puerta ¿Tocas el timbre (1) o Golpeas la puerta (2)?”);
if (Input.GetKeyDown(“1”)) //SITUACIÓN1
{
Debug.Log(“Se abre una compuerta secreta debajo y caes al sótano”);
Debug.Log(“Pierdes una vida. Te quedan 2 vidas”);
Debug.Log(“En el sótano hay un gallo y una puerta.”);
Debug.Log(“¿Lo levantas (1) o Lo llamas (2)?”);
if (Input.GetKeyDown(“1”)) //SITUACIÓN2
{
Debug.Log(“Te da un picotazo.”);
Debug.Log(“Pierdes una vida. Te queda 1 vida…”);
Debug.Log(“El gallo cacarea, alguien arriba lo escucha y baja por las escaleras…”);
Debug.Log(“¿Te escondes (1) o Te quedas quieto (2)?”);
if (Input.GetKeyDown(“1”))//SITUACIÓN3
{
Debug.Log(“Nadie te encuentra jamás.”);
Debug.Log(“Te da demasiada hambre.”);
Debug.Log(“Pierdes tu última vida.”);
Debug.Log(“PERDISTE…”);
}
if (Input.GetKeyDown(“2”))//SITUACIÓN3
{
Debug.Log(“Llega un viejo con una escopeta y te dispara.”);
Debug.Log(“xd”);
Debug.Log(“Pierdes tu última vida.”);
Debug.Log(“PERDISTE…”);
}
When i run it, and i press like 1, the 1 continues til the end of the game…
Help please