Error A namespace cannot directly contain members, such as fields or methods

Asked

Viewed 68 times

-2

Mine’s making that mistake twice. here is my command:

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

public class Porta : MonoBehaviour {
    private Animator porta;
    private bool colidindo;
    // Start is called before the first frame update
    void Start() {
        porta = GetComponent<Animator> ();
        porta.SetInteger ("abreFecha", 0);
        colidindo = false;
    }

    // Update is called once per frame
    void Update() {
        if ((Input.GetKeyDown (KeyCode.E)) && (colidindo = true)){
            if ((porta.GetCurrentAnimatorStateInfo (0).IsName ("portaParada"))  (porta.GetCurrentAnimatorStateInfo (0).IsName ("fechaPorta"))){
                porta.SetInteger ("abreFecha", 1);
            } else {
                porta.SetInteger ("abreFecha", 2);   
            }

        }

    }
}
    void OnTriggerEnter (Collider other)
{
        if (other.gameobject.CompareTag("Player")){
                    colidindo = true;
        }
    }
    void OnTriggerExit (Collider other){

        if (other.gameobject.CompareTag("Player")){
                    colidindo = false;
        }
    }

1 answer

0

I believe the problem occurs because the method OnTriggerEnter and OnTriggerExit is outside the scope of the Door class.

Following corrected example.

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

public class Porta : MonoBehaviour {
    private Animator porta;
    private bool colidindo;
    // Start is called before the first frame update
    void Start() {
        porta = GetComponent<Animator> ();
        porta.SetInteger ("abreFecha", 0);
        colidindo = false;
    }

    // Update is called once per frame
    void Update() {
        if ((Input.GetKeyDown (KeyCode.E)) && (colidindo = true)){
            if ((porta.GetCurrentAnimatorStateInfo (0).IsName ("portaParada"))  (porta.GetCurrentAnimatorStateInfo (0).IsName ("fechaPorta"))){
                porta.SetInteger ("abreFecha", 1);
            } else {
                porta.SetInteger ("abreFecha", 2);   
            }

        }

    }

    void OnTriggerEnter (Collider other)
    {
        if (other.gameobject.CompareTag("Player")){
                    colidindo = true;
        }
    }
    void OnTriggerExit (Collider other){

        if (other.gameobject.CompareTag("Player")){
                    colidindo = false;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.