简体   繁体   English

无法将类型“UnityEngine.GameObject”隐式转换为“GameObject”

[英]Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameObject'

Hello everyone and thanks in advance for your answers!大家好,提前感谢您的回答!

I am a beginner in unity, coding my second game after i finshed a couple of tutorials.我是一个统一的初学者,在完成几个教程后编写我的第二个游戏。 Since today, i noticed that suddenly all my "GameObjects" have a "UnityEngine."从今天开始,我突然注意到我所有的“游戏对象”都有一个“UnityEngine”。 in front of them.在他们面前。

I have no idea how that happened, and it is also not just in one script, but all of them.我不知道这是怎么发生的,而且它不仅在一个脚本中,而且在所有脚本中。 Heres an example of it:这是它的一个例子:

UnityEngine.GameObject item = (UnityEngine.GameObject)Instantiate(itemGOList[i], spawnTransform, spawnRotation);

This worked fine before without the "UnityEngine.", but now it only works when its written this way.这在没有“UnityEngine.”的情况下工作得很好,但现在它只有在以这种方式编写时才有效。

Do you know how this could have happened and how to revert it?你知道这是怎么发生的以及如何恢复它吗?

This is one of the scripts:这是脚本之一:

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.
    }

   
}

The problem was that i had a script called "GameObject" in my Unity Project.问题是我的 Unity 项目中有一个名为“GameObject”的脚本。

I was also able to rename all "UnityEngine.GameObject" to "Gameobject" by using the Quick Action in Visual Studio.通过使用 Visual Studio 中的快速操作,我还能够将所有“UnityEngine.GameObject”重命名为“Gameobject”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型&#39;Vuforia.Anchor&#39;隐式转换为&#39;UnityEngine.GameObject&#39; - Cannot implicitly convert type `Vuforia.Anchor' to 'UnityEngine.GameObject' 无法将类型“UnityEngine.GameObject”隐式转换为“GameMaster”? - Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameMaster'? 无法将类型“UnityEngine.Transform”转换为“UnityEngine.GameObject” - Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject' 如何将类型“int”转换为“unityengine.gameobject”? - How to convert type 'int' to 'unityengine.gameobject'? foreach语句无法遍历UnityEngine.GameObject类型的变量 - foreach statement cannot ooperate on variables of type UnityEngine.GameObject SerializationException:类型UnityEngine.GameObject未标记为Serializable - SerializationException: Type UnityEngine.GameObject is not marked as Serializable 无法转换 Type 'UnityEngine.Rigidbody' en 'UnityEngine.GameObject 并且没有 Addforce 的定义 - Can't convert Type 'UnityEngine.Rigidbody' en 'UnityEngine.GameObject and no definition for Addforce 错误CS0021:无法将带有[]的索引应用于类型为&#39;UnityEngine.GameObject&#39;的表达式 - Error CS0021: Cannot apply indexing with [] to an expression of type `UnityEngine.GameObject' UnityEngine.GameObject与Generic.List <UnityEngine.GameObject> - UnityEngine.GameObject vs. Generic.List<UnityEngine.GameObject> “UnityEngine.GameObject[]”不包含“Transform”错误的定义 - 'UnityEngine.GameObject[]' does not contain a definition for `Transform' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM