因为这样的组件已经添加到游戏对象中

因为这样的组件已经添加到游戏对象中

本文介绍了无法将组件“动画"添加到testObj,因为这样的组件已经添加到游戏对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将脚本的Update()函数中的AddoCompornent"Animation"添加到testObj.

I want to AddoCompornent "Animation" in the Update() function of the script to testObj.

我编写并执行了以下代码,但出现错误.

I wrote and executed the following code, but I get an error.

using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System;

public class unityRecieve : MonoBehaviour
{
    // Start is called
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        GameObject obj = GameObject.Find("testObj");
        obj.AddComponent<Animation>();
    }
}

错误详细信息如下.

Can't add component 'Animation' totestObj because such a component is already added to the game object!

如果已将动画组件添加到游戏对象中,如何跳过该处理?

How can I skip the processing if the Animation Compornent has already been added to the Gameobject?

我需要动态重命名"testObj",所以我想在Update()函数中而不是在Startup()函数中处理它.

I need to rename "testObj" dynamically, so I want to handle it in the Update () function instead of in the Startup () function.

我想使用if语句检查动画组件是否已经添加到testObj之后,再添加组件,如下所示.但是,如果使用以下方法,则会发生错误.我不知道正确的方法.

I want to AddComponent after checking if Animation component is already added to testObj using if statement as follows. However, an error occurs if the following method is used. I don't know the correct way.

using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System;

public class unityRecieve : MonoBehaviour
{
    public Animation animation;

    // Start is called
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        GameObject obj = GameObject.Find("testObj");
        animation = GetComponent<Animation>(obj);
        if(animation == null)
        {
            obj.AddComponent<Animation>();
        }
    }
}

推荐答案

我说异常消息是不言自明的.

I'ld say the exception message is self-explanatory enough.

您当然可以简单地使用

if(!obj.GetComponent<Animation>())
    obj.AddComponent<Animation>();


但是,如注释中所述,FindGetComponent都是非常昂贵的调用,并且从不应该在Update中进行.通常,您应尽可能避免使用它们.


However, as mentioned in the comments both Find and GetComponent are very expensive calls and should never be done in Update. In general you should avoid them as much as possible.

例如,另一种可能是

无效Update() { if(isInitialized)返回;

void Update() { if(isInitialized) return;

    var obj = GameObject.Find("testObj");
    obj.AddComponent<Animation>();
    isInitialized = true;
}

但这也效率很低.您应该只一次并且仅在收到UDP命令时执行此操作.

But also this is quite inefficient. You should rather simply do it only once and only when the UDP command is received.

我仍然不明白为什么你不能

I still don't see the reason why you can't

    甚至在启动应用程序之前,默认情况下
  • 通过检查器将Animation添加到GameObject
  • 创建一个已经连接了Animation的预制件(通过检查器)
  • 仅在Start一次使用Find并将其引用存储在字段中

  • add the Animation to the GameObject via the Inspector by default before even starting the App
  • create a Prefab which already has the Animation attached (via the Inspector )
  • use Find only once in Start and store the reference to it in a field

通常已经通过检查器在字段中引用了obj

in general already reference the obj in a field via the Inspector

这篇关于无法将组件“动画"添加到testObj,因为这样的组件已经添加到游戏对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:23