Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法隐式转换类型';游戏对象&x27;至';游戏对象';_C#_Unity3d - Fatal编程技术网

C# 无法隐式转换类型';游戏对象&x27;至';游戏对象';

C# 无法隐式转换类型';游戏对象&x27;至';游戏对象';,c#,unity3d,C#,Unity3d,大家好,提前谢谢你们的回答 我是unity的初学者,在完成了几篇教程之后,我编写了第二个游戏。 从今天开始,我突然发现我所有的“游戏对象”前面都有一个“UnityEngine” 我不知道这是怎么发生的,而且不仅仅是一个剧本,而是所有的剧本。下面是一个例子: UnityEngine.GameObject item = (UnityEngine.GameObject)Instantiate(itemGOList[i], spawnTransform, spawnRotation); 在没有“Uni

大家好,提前谢谢你们的回答

我是unity的初学者,在完成了几篇教程之后,我编写了第二个游戏。 从今天开始,我突然发现我所有的“游戏对象”前面都有一个“UnityEngine”

我不知道这是怎么发生的,而且不仅仅是一个剧本,而是所有的剧本。下面是一个例子:

UnityEngine.GameObject item = (UnityEngine.GameObject)Instantiate(itemGOList[i], spawnTransform, spawnRotation);
在没有“UnityEngine”的情况下,它以前工作得很好,但现在它只有在以这种方式编写时才能工作

你知道这是怎么发生的吗?你知道如何恢复它吗

这是其中一个脚本:

using UnityEngine;
using UnityEngine.EventSystems;

public class Turret : MonoBehaviour
{

    private Transform target;
    private Enemy targetEnemy;

    [Header("General")]

    public float range = 10f;

    [Header("Use Bullets/missiles (default)")]
    public UnityEngine.GameObject bulletPrefab;
    public float fireRate = 1f;
    private float fireCountdown = 0f;

    public AudioClip shootingSound;
    private AudioSource audioSource;

    [Header("Use Laser (default)")]
    public bool useLaser = false;

    public int damageOverTime = 20;

    public float slowAmount = .5f;

    public LineRenderer lineRenderer;
    public ParticleSystem impactEffect;
    public Light impactLight;

    [Header("Unity Setup Fields")]


    public string enemyTag = "Enemy";

    public Transform partToRotate;
    public float turnSpeed = 10f;

    public Transform firePoint;

    void Start()
    {
        InvokeRepeating(nameof(UpdateTarget), 0f, 0.3f); // Call the UpdateTarget Method after 0 seconds, then repeat every 0.3 seconds.
        audioSource = GetComponent<AudioSource>(); //Puts the AudioSource of this GO (the turret) into the variable audioSource.
    }

    void UpdateTarget ()
    {
        UnityEngine.GameObject[] enemies = UnityEngine.GameObject.FindGameObjectsWithTag(enemyTag); // Find all enemies (and store in a list?).
        float shortestDistance = Mathf.Infinity; // Create a float for the shortest distance to an enemy.
        UnityEngine.GameObject nearestEnemy = null; // Create a variable which stores the nearest enemy as a gameobject.

        foreach (UnityEngine.GameObject enemy in enemies) // Loop through the enemies array.
        {
            float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position); //Get the distance to each enemy and stores it.
            if (distanceToEnemy < shortestDistance) // If any of the enemies is closer than the original, make this distance the new shortestDistance and the new enemy to the nearestEnemy.
            {
                shortestDistance = distanceToEnemy;
                nearestEnemy = enemy;
            }
        }
        if (nearestEnemy != null && shortestDistance <= range) // Sets the target to the nearestEnemy (only if its in range and not null).
        {
            target = nearestEnemy.transform;
            targetEnemy = nearestEnemy.GetComponent<Enemy>();
        }
        else
        {
            target = null;
        }
    }

    void Update()
    {
            if (target == null) // If there is no target, do nothing.
        {
            if (useLaser)
            {
                if (lineRenderer.enabled)
                {
                    lineRenderer.enabled = false;
                    impactEffect.Stop();
                    impactLight.enabled = false;
                    audioSource.Stop();
                }
            }
            return;
        }
        LockOnTarget();

        if (useLaser)
        {
            Laser();
        }
        else
        {
            if (fireCountdown <= 0f)
            {
                Shoot();
                fireCountdown = 1f / fireRate;
            }

            fireCountdown -= Time.deltaTime;
        }

        
    }

    void Laser()
    {
        targetEnemy.TakeDamage(damageOverTime * Time.deltaTime);
        targetEnemy.Slow(slowAmount);

        if (!lineRenderer.enabled)
        {
            lineRenderer.enabled = true;
            impactEffect.Play();
            impactLight.enabled = true;
            if (audioSource.isPlaying == false)
            {
                audioSource.Play();
            }
        }
        lineRenderer.SetPosition(0, firePoint.position);
        lineRenderer.SetPosition(1, target.position);

        Vector3 dir = firePoint.position - target.position;

        impactEffect.transform.position = target.position + dir.normalized * 1f;

        impactEffect.transform.rotation = Quaternion.LookRotation(dir);
    }

    void LockOnTarget()
    {
        Vector3 dir = target.position - transform.position; // Store the direction from turret to target.
        Quaternion lookRotation = Quaternion.LookRotation(dir); // I have no idea how this works...
        Vector3 rotation = Quaternion.Lerp(partToRotate.rotation, lookRotation, Time.deltaTime * turnSpeed).eulerAngles; // Convert quaternion angles to euler angles and Lerp it (smoothing out the transition).
        partToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f); // Only rotate around the y-axis.
    }
    
    void Shoot()
    {
        UnityEngine.GameObject bulletGO = (UnityEngine.GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); // Spawn a bullet at the location and rotation of firePoint. (Also makes this a GO to reference it.
        Bullet bullet = bulletGO.GetComponent<Bullet>(); // I have no idea how this works...
        audioSource.PlayOneShot(shootingSound, 0.2f);


        if (bullet != null) // If there is a bullet, use the seek method from the bullet script.
        {
            bullet.Seek(target);
        }

    }
    void OnDrawGizmosSelected() // Do this method if gizmo is selected.
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, range); // Draw a wire sphere on the selected point (selected turret f. e.) and give it the towers range.
    }

   
}
使用UnityEngine;
使用UnityEngine.EventSystems;
公共级炮塔:单一行为
{
私有转换目标;
私人敌人目标敌人;
[标题(“一般”)]
公共浮动范围=10f;
[标题(“使用子弹/导弹(默认)”)]
公共UnityEngine.GameObject BulletPrefact;
公共浮式消防车的火灾率=1f;
私人浮动消防倒计时=0f;
公共音频剪辑射击声;
私人音频源音频源;
[标题(“使用激光(默认)”)]
公共广播使用激光=假;
公众伤害加班费=20;
公共浮点数的slowAmount=0.5f;
公共线条渲染器线条渲染器;
公共参与制度冲击效应;
公共照明;
[标题(“统一设置字段”)]
公共字符串enemyTag=“敌人”;
公共变换部分旋转;
公共浮子转速=10f;
公共转换点;
void Start()
{
InvokeRepeating(nameof(UpdateTarget),0f,0.3f);//在0秒后调用UpdateTarget方法,然后每0.3秒重复一次。
audioSource=GetComponent();//将此GO(炮塔)的音频源放入变量audioSource中。
}
void UpdateTarget()
{
UnityEngine.GameObject[]敌人=UnityEngine.GameObject.FindGameObjectsWithTag(enemyTag);//查找所有敌人(并存储在列表中?)。
float shortestDistance=Mathf.Infinity;//为到敌人的最短距离创建一个float。
UnityEngine.GameObject nearestEnemy=null;//创建一个变量,将最近的敌人存储为游戏对象。
foreach(UnityEngine.GameObject敌方中的敌人)//循环通过敌人阵列。
{
float Distance toenemy=Vector3.Distance(transform.position,敌方.transform.position);//获取到每个敌方的距离并存储它。
if(distanceToEnemy如果(nearestEnemy!=null&&shortestDistance,问题是我的Unity项目中有一个名为“GameObject”的脚本


我还可以将所有“UnityEngine.GameObject”重命名为“GameObject”通过在Visual Studio中使用快速操作。

'unityengine'是命名空间的一部分,您的项目中是否有第二个名为完全相同的类,
GameObject
?命名空间将添加到类之前以使其更具体。例如,如果我有
MyNamespace.MyClass
OtherNamespace.MyClass
-I使用
MyClass
时需要添加名称空间,以便编译器知道我的意思是哪个
MyClass
。您是否创建了自己的名为
GameObject
的类型?进入Unity并在ProjectView中搜索
GameObject
,谢谢大家!我不知道为什么,但其中一个脚本突然被称为GameObject.Del现在,我如何将所有UnityEngine.GameObject还原为GameObject?另外,我如何将此答案标记为最佳答案?好的,我明白了,我想我可以让visual studio在整个项目中为我编辑所有UnityEngine.GameObjects!