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

Question can't build my game

Discussion in 'Scripting' started by faisalzx280, Jun 23, 2020.

  1. faisalzx280

    faisalzx280

    Joined:
    Jun 1, 2020
    Posts:
    4
    i have two scene and after try to build but can't . this two messages below showing

    here two messages:
    1
    ((
    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 'Player1' on game object 'player-idle-1'.
    See "Script Serialization" page in the Unity Manual for further details.
    Player1..ctor () (at Assets/Scripts/Player1.cs:20)
    ))

    2
    ((

    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 'Player1'.
    See "Script Serialization" page in the Unity Manual for further details.
    Player1..ctor () (at Assets/Scripts/Player1.cs:20)

    ))





    finally this my code below:




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Security.AccessControl;
    4. using UnityEngine;
    5.  
    6. public class Player1 : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField]
    10.     float speed = 0.03f;
    11.     [SerializeField]
    12.     float jump = 10f;
    13.     bool JumpOnce;
    14.     Animator anim;
    15.     Transform myTransform;
    16.     SpriteRenderer mySprite;
    17.     Rigidbody2D rb;
    18.     public bool onLadder = false;
    19.     public float gravityScale = 5;
    20.     float vDirection = Input.GetAxis("Vertical");
    21.  
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         myTransform = GetComponent<Transform>();
    27.         mySprite = GetComponent<SpriteRenderer>();
    28.         rb = GetComponent<Rigidbody2D>();
    29.         anim = GetComponent<Animator>();
    30.         vDirection = Input.GetAxis("Vertical");
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.        
    37.         float hDirection = Input.GetAxis("Horizontal");  
    38.         if (hDirection > 0)
    39.  
    40.         {
    41.             anim.SetBool("isRunning", true);
    42.             transform.Translate(new Vector3(1 * speed, 0, 0));
    43.             mySprite.flipX = false;
    44.         }
    45.  
    46.         else if (hDirection < 0)
    47.        
    48.         {
    49.             anim.SetBool("isRunning", true);
    50.             transform.Translate(new Vector3(-1 * speed, 0, 0));
    51.             mySprite.flipX = true;
    52.         }
    53.         else { anim.SetBool("isRunning", false);
    54.         }
    55.  
    56.  
    57.      
    58.  
    59.         if (Input.GetButtonDown("Jump") && JumpOnce == false)
    60.         {
    61.             rb.velocity = new Vector2(0, jump);
    62.             JumpOnce = true;
    63.             anim.SetTrigger("isJumping");
    64.  
    65.         }
    66.  
    67.         if (Mathf.Abs(rb.velocity.y) < 0.03f)
    68.         {
    69.             JumpOnce = false;
    70.             anim.SetFloat("Speed", Mathf.Abs(rb.velocity.x));
    71.         }
    72.    
    73.  
    74.  
    75.     }
    76. }
    77.  
     

    Attached Files:

    • fox.jpg
      fox.jpg
      File size:
      470.5 KB
      Views:
      274
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,926
    The message is correct. Do what it says:

     
  3. faisalzx280

    faisalzx280

    Joined:
    Jun 1, 2020
    Posts:
    4
    there was asset file inside project (not used) when delete it its work
    Thank you Owen-Reynolds