为什么在创建新的GameObjects时没有更改标签? [英] Why when creating new GameObjects it's not changing the tag?

查看:93
本文介绍了为什么在创建新的GameObjects时没有更改标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第一个脚本中:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InstantiateObjects : MonoBehaviour
{
    public GameObject prefab;
    public Terrain terrain;
    public float yOffset = 0.5f;

    private float terrainWidth;
    private float terrainLength;

    private float xTerrainPos;
    private float zTerrainPos;


    void Start()
    {
        //Get terrain size
        terrainWidth = terrain.terrainData.size.x;
        terrainLength = terrain.terrainData.size.z;

        //Get terrain position
        xTerrainPos = terrain.transform.position.x;
        zTerrainPos = terrain.transform.position.z;

        //generateObjectOnTerrain();
    }

    public void generateObjectOnTerrain(bool parent, string tag)
    {
        //Generate random x,z,y position on the terrain
        float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
        float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
        float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));

        //Apply Offset if needed
        yVal = yVal + yOffset;

        //Generate the Prefab on the generated position        
        GameObject objInstance = Instantiate(prefab, new Vector3(randX, yVal, randZ), Quaternion.identity);
        if (parent)
            objInstance.transform.parent = this.transform;
        objInstance.transform.tag = tag;
    }
}

在我正在使用的脚本中:

And in the script that i'm using this:

private void Start()
    {
        for (int i = 0; i < cloneTeleportations; i++)
        {
            InstantiateObjects gos = GetComponent<InstantiateObjects>();
            gos.prefab = prefab;
            gos.generateObjectOnTerrain(true, "ddd");//"Teleportation");
        }
    }

如果我将其从true更改为false,则不会使GameObjects成为子对象,如果为true,则它们将为子对象.父部分正在工作. 但是为了进行测试,我尝试将标签更改为"ddd",并且在第一个脚本中看到了标签为"ddd":

If i will change it from true to false it will not make the GameObjects childs and if it's true they will be childs. The parent part is working. But for testing i tried to change the tag to "ddd" and i saw in the first script that the tag is "ddd":

objInstance.transform.tag = tag;

当运行游戏时,所有被标记为传送"而不是"ddd"的克隆游戏对象,

标记为"ddd",默认情况下,objInstance.transform.tag为传送".

tag is "ddd" and objInstance.transform.tag by default is "Teleportation" when running the game all the cloned gameobjects tagged as "Teleportation" and not "ddd".

推荐答案

Unity中,您必须首先从编辑器中手动添加tag,然后才能将其分配给GameObject.如果tag不存在,则无法分配它.

In Unity you must first add a tag manually from the Editor and only then you will be able to assign it to a GameObject. If the tag does not exist it's not possible to assign it.

这篇关于为什么在创建新的GameObjects时没有更改标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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