简体   繁体   中英

error CS0138: A 'using namespace' directive can only be applied to namespaces; 'Collision2D'

I tried to make a script for moving platforms, where Player can stay "blocked" on it.But when i saved the script, it showed me this: `error CS0138: A 'using namespace' directive can only be applied to namespaces; 'Collision2D' is a type not a namespace. Consider a 'using static' directive instead

The script is :

using UnityEngine;
using UnityEngine.Collision2D;

public class OnMovingPlat : MonoBehaviour
{

    [SerializeField]
    private Vector3 velocity;

    private bool moving;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.WasWithPlayer())
        {
            moving = true;
            collision.collider.transform.SetParent(transform);
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.WasWithPlayer())
        {
            collision.collider.transform.SetParent(null);
        }
    }

    private void FixedUpdate()
    {
        if (moving)
        {
            transform.position += (velocity * Time.deltaTime);
        }
    }
}

What can i do ?

Seams Collision2D becomes a Type. you may accidentally create a class name called "Collision2D" under "UnityEngine" namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM