激活非活动的同名异标记游戏对象 [英] Activating Inactive Same-Name Different-Tag Game Objects

查看:172
本文介绍了激活非活动的同名异标记游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法更改游戏对象(化身)的名称,但需要具有2个不同的版本并通过代码激活它们.

For some reason, I cannot change the name of a game object (an avatar) but I need to have 2 different versions of it and activate them in code.

因此,复制头像不是一种选择,但是我仍然需要创建一个相同的头像并在编辑器模式下进行更改,以便根据条件,一个另一个被选中.

So, duplicating the avatar is not an option, but I still need to create an identical one and make a change in editor mode, so that based on a condition, whether one or the other is chosen.

我想到了,现在无法更改名称了,也许我可以给另一个一个不同的标签,然后根据所选方案激活正确的一个

It occurred to me, now that changing the name is not possible, perhaps I can give the other a different tag and activate the right one based on the selected scenario.

因此,我查看了一些手册文档和一些论坛帖子,并提出了以下3种替代方法放置在我的Start()中,但没有用:

So, I checked out a few manual docs and some forum posts and came up with the following 3 alternatives to place in my Start() but none works:

void Start()
{
    if ( GameObject.FindGameObjectWithTag("John") )
        gameObject.FindGameObjectWithTag("John").SetActive(true);

    if ( GameObject.FindWithTag("John") )
        gameObject.FindWithTag("John").SetActive(true);
}

有人可以帮助我了解我在做什么,以及如何实现这一目标:找到一个不活动的对象(是从两个名称相同但不同的不活动对象中进行的标签:一个untagged,另一个被标记为John)

Can someone please help me understand what I am doing wrong and how I can achieve this: find an object that is not active (from among 2 inactive objects with the same name but different tags: one untagged and the other tagged as, say, John)

推荐答案

再简单不过了,只需将它们分配给 不同的变量

Couldn't be easier, just assign them to different variables

public GameObject johnCharacterOnLeft;
public GameObject johnCharacterWhoJoinsLater;

void Start()
  {
  johnCharacterOnLeft.SetActive(true);
  johnCharacterOnLeft.Whatever(true);
  johnCharacterOnLeft.Whatever(true);
  johnCharacterOnLeft.Whatever(true);
  johnCharacterWhoJoinsLater.SetActive(false);
  johnCharacterWhoJoinsLater.Whatever(false);
  johnCharacterWhoJoinsLater.Whatever(false);
  ...
  }

请注意,通常在学习时, 请勿 使用查找.就这么简单.使用公共变量并拖动以连接检查器.

Note, as a rule when you are learning, do not use Find. It's that simple. Use public variables and drag to connect in the inspector.

(如果任何阅读者尚不知道如何在检查器中拖动以进行连接,请检查所有基本的教程.)

(If anyone reading does not yet know how to drag to connect in the inspector, check any basic tutorial.)

GameObject[] allCats;
allCats = GameObject.FindGameObjectsWithTag("cat");
foreach (GameObject g in allCats)
 Debug.Log("I found one, name is: " +g.name);

如果要查找具有特定名称的之一,则必须全部搜索.

If you want to find one of those with a certain name you have to search through them all..

GameObject[] allCats;
allCats = GameObject.FindGameObjectsWithTag("cat");
foreach (GameObject g in allCats)
 {
 Debug.Log("I found one, name is: " +g.name);
 if ( g.name == "Felix" )
   {
   Debug.Log("I did find Felix the cat");
   g.Whatever ...
   }


如果您要查找具有特定名称 ...

all 个对象

了解这一点非常重要


If you do want to find all objects with a certain name...

It's very important to understand that

一生中的生活-Find是一种骇客,除非在极少数情况下,否则您绝对不应该真正使用它.如果您(出于某种原因?)需要查找"所有具有特定名称的游戏对象,您只需浏览场景中的每件事(这并非易事).您可以轻松地用谷歌搜索.但是,绝对没有理由您会这样做.

Life's life that - Find is a hack you should never really use anyway, other than in rare situations. If you (for some reason?) need to "find" all game objects with a certain name, you have to simply look through every single thing in the scene (which is not particularly easy to do). You can easily google this. But there is, absolutely, no reason you would ever do this.

请注意,只是一个例子 ..说您有许多猫"猫,场景中的事物. 非常简单-没有什么比这更复杂-您可以将它们全部放在同一个文件夹"中 . (也就是说,所有对象都在同一个持有者游戏对象下.)这只是一种非常简单的方法,可以完全避免您遇到的这类问题.

Note that, just one example .. say you had a number of "cat" thing in your scene. Very simply - nothing more complicated than this - you would have them all in the same "folder". (That is to say, all under the same holder game object.) That is just one incredibly simple approach to utterly avoid the sort of problem you're having.

再次在那里根本找不到所有具有相同名称的游戏对象" -Find调用是本不应该在Unity中使用的hack;他们明确没有

Again there literally is no way to find "all game objects with the same name" - the Find call is a hack that should never have been in Unity; they explicitly don't have

Find调用是一种本不应该在Unity中使用过的hack:永远记住它甚至对不活动的对象也不起作用.因为他们把找到的名字"丢进去了.概念,最好是对不活动的对象不起作用:但是整个过程都是一团糟-永远不要使用查找".

The Find call is a hack that should never have been in Unity: always remember it does not even work on inactive objects. Inasmuch as they threw in a "find name" concept, it's probably for the best that it doesn't work on inactive objects: but the whole thing is a shambles - just don't use "find", ever.

特别是作为初学者 只需将鼠标拖入检查器中 -坦白说,这很容易. (同样,在几乎所有情况下,您的东西都会开始处于非活动状态,因此您当然必须这样做,因为您无法找到"不活动的东西.)

Particularly as a beginner just drag in the inspector - it's honestly that easy. (And again, in almost all cases your stuff will start inactive, so of course you have to do that since you can't "find" inactive stuff.)

(还有一个注意事项-不要忘记,如果您只是在查找老板"类型的脚本,例如说您的ScoresSoundEffects,请不要使用find.只需执行

(And a note - don't forget that if you're simply finding a "boss" type script, say your Scores or SoundEffects, never use find. Just do this easy idiom. )

这篇关于激活非活动的同名异标记游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆