1

Assets/Scripts/Bird.cs(40,13): error CS0120: An object reference is required to access non-static member t.Stop();

using UnityEngine;
using System.Collections;
using System.Timers;
public class Bird : MonoBehaviour {
    // Movement speed
    public float speed = 1;


    // Flap force
    public float force = 100;

    public double Interval = 10000;

    private Timer t;

    // Use this for initialization
    void Start () {
      // Fly towards the right
      GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
      t = new Timer(Interval);
      t.Elapsed += T_Elapsed;
      t.Start();
  }

    // Update is called once per frame
    void Update () {
        // Flap
        if (Input.GetKeyDown(KeyCode.Space))
            GetComponent<Rigidbody2D>().AddForce(Vector2.up * force);
    }

    void OnCollisionEnter2D(Collision2D coll) {
        // Restart
        t.Stop();
        Application.LoadLevel(Application.loadedLevel);
    }

    private static void T_Elapsed(object sender, ElapsedEventArgs e)
        {
            t.Stop();
            ///
            /// aqui pones el codigo para mostrar que gano
            ///
        }
}

1 respuesta 1

1

El error que tienes es debido a que estás intentando acceder a una variable de la clase desde un método estático.

Deberías declarar la función como no estática para solucionar el error:

private void T_Elapsed(object sender, ElapsedEventArgs e)
1
  • Muchas gracias Marc
    – kit
    el 1 abr. 2019 a las 13:10

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.