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

Ayuda!! me tira este error!! Gracias

Discussion in '2D' started by Noelia88, May 6, 2022.

  1. Noelia88

    Noelia88

    Joined:
    Jan 17, 2022
    Posts:
    3

    NullReferenceException: Object reference not set to an instance of an object
    ControlPersonaje.Update () (at Assets/ControlPersonaje.cs:38)
    38>>(Hacha.GetComponent<CircleCollider2D>().enabled = false;)



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

    public class ControlPersonaje : MonoBehaviour {
    Rigidbody2D rgb;
    Animator anim;
    public float maxVel = 5f;
    bool haciaDerecha = true;

    public Slider slider;
    public Text txt;

    public float energy=100;

    public int costoGolpeAlAire = 1;
    public int costoGolpeAlArbol = 3;
    public int premioArbol = 15;


    bool enFire1 = false;

    ControlArbol ctrArbol=null;
    public GameObject Hacha=null;


    void Start() {
    rgb = GetComponent<Rigidbody2D> ();
    anim = GetComponent<Animator> ();
    Hacha = GameObject.Find ("/orco/orc_body/orc_R_arm/orc_R_hand/orc_weapon");
    }

    void Update() {
    if (Mathf.Abs (Input.GetAxis ("Fire1")) > 0.01f) {
    if (enFire1 == false) {
    enFire1 = true;
    Hacha.GetComponent<CircleCollider2D>().enabled = false;
    anim.SetTrigger ("attack");
    if (ctrArbol != null) {
    if (ctrArbol.GolpeOrco () ) {
    energy += premioArbol;
    if (energy > 100)
    energy = 100;
    }
    else {
    energy -= costoGolpeAlArbol;
    }
    }
    else {
    energy -= costoGolpeAlAire;
    }
    }
    } else {
    enFire1 = false;
    }

    slider.value = energy;
    txt.text = energy.ToString ();

    }

    public void HabilitarTriggerHacha() {
    Hacha.GetComponent<CircleCollider2D>().enabled = true;
    }

    void FixedUpdate() {
    float v = Input.GetAxis ("Horizontal");
    Vector2 vel = new Vector2 (0, rgb.velocity.y);
    v *= maxVel;
    vel.x = v;
    rgb.velocity = vel;

    anim.SetFloat ("speed",vel.x);

    if(haciaDerecha && v < 0) {
    haciaDerecha = false;
    Flip();
    }
    else if(!haciaDerecha && v>0) {
    haciaDerecha = true;
    Flip();
    }
    }

    void Flip() {
    var s = transform.localScale;
    s.x *= -1;
    transform.localScale = s;
    }

    public void SetControlArbol (ControlArbol ctr) {
    ctrArbol = ctr;
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
  3. Noelia88

    Noelia88

    Joined:
    Jan 17, 2022
    Posts:
    3
    Gracias por la respuesta!, estoy realizando un curso básico de Unity pero no tengo a quien preguntarle donde me estoy equivocando....sigo sin encontrar como arreglarlo :(