错误CS0029:无法隐式转换类型`UnityEngine.GameObject []'到'UnityEngine.GameObject'

时间:2015-08-26 11:19:27

标签: c# unity3d

我收到错误:

lad = GameObject.FindGameObjectsWithTag ("Ladder");

我只是在闹钟开启时尝试禁用场景中的所有梯子,然后在闹钟消失后再次启用它们。我不确定错误意味着什么,但需要尽快修复。我使用的是C#和Unity 5.
感谢您提供的任何帮助!

using UnityEngine;
using System.Collections;

public class LevelAlarm : MonoBehaviour {

    public bool alarmOn       = false;
    public bool bPlayerHidden = false;

    // How long an alarm lasts for
    [SerializeField]
    private float alarmTimer = 5.0f;

    private float alarmCurrentTimer = 0.0f;


    public GameObject lad;

    // Use this for initialization
    void Start () 
    {
        lad = GameObject.FindGameObjectsWithTag ("Ladder");


    }

    // Update is called once per frame
    void Update () 
    {
        // While alarm is on, run a timer
        if (alarmOn) 
        {
            alarmCurrentTimer += Time.deltaTime;

            lad.SetActive(false);

            // Timer is complete, alarm resets
            if(alarmCurrentTimer >= alarmTimer)
            {
                alarmOn = false;
                alarmCurrentTimer = 0.0f;

                lad.SetActive(true);
            }
        }

    }
}

3 个答案:

答案 0 :(得分:4)

_ o _ p _ t _ _ _ a b b a g _ b a n a n a 返回GameObject.FindGameObjectsWithTag()数组,如果你想要这个数组的第一个对象,请使用:

GameObjects

答案 1 :(得分:1)

它是GameObject.FindGameObjectWithTag,而不是GameObject.FindGameObjectsWithTag,在“ Object” 之后没有字符“ s”

答案 2 :(得分:0)

错误CS0029:无法隐式转换类型UnityEngine.GameObject[]' to UnityEngine.GameObject' 此错误只是意味着您尝试停止其标记为“Ladder”的警报。它创建一个数组,您使用的函数只接受utilityEngine.GameObject的一个元素。因此,您可以使用foreach循环来访问数组中的所有元素:

public bool alarmOn       = false;
public bool bPlayerHidden = false;

// How long an alarm lasts for
[SerializeField]
private float alarmTimer = 5.0f;

private float alarmCurrentTimer = 0.0f;


**public GameObject[] lad;**

// Use this for initialization
void Start () 
{
    lad = GameObject.FindGameObjectsWithTag ("Ladder");


}

// Update is called once per frame
void Update () 
{
    // While alarm is on, run a timer
    if (alarmOn) 
    {
        alarmCurrentTimer += Time.deltaTime;
**foreach(var eachLad in lad)
{
eachLad.SetActive(false);
}**


        // Timer is complete, alarm resets
        if(alarmCurrentTimer >= alarmTimer)
        {
            alarmOn = false;
            alarmCurrentTimer = 0.0f;
**foreach(var eachLad in lad)
{
           eachLad.SetActive(true);
}**

        }
    }

}