Why when creating new GameObjects it's not changing the tag? [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/39374977/unityexception-tag-is-not-defined" dir="ltr" rel="nofollow">UnityException: Tag: is not defined</a> <span class="question-originals-answer-count"> 1 answer </span> </li> <li> <a href="/questions/43988243/why-when-instantiate-new-gameobjects-its-not-adding-tag-to-them" dir="ltr" rel="nofollow">Why when Instantiate new GameObjects it's not adding tag to them? [duplicate]</a> <span class="question-originals-answer-count"> 1 answer </span> </li> </ul>

In the first script:

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"); } }

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;

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


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.


  • Add geom_rug like boxplots per group in ggplot2
  • Clone selected element
  • Multiple fancybox google map
  • How to perform single factor ANOVA in R with samples organized by column?
  • How to escape a '#' so it will NOT reference an issue in a GitHub comment?
  • Hibernate out of memory exception while processing large collection of elements
  • Radio button value not in $_POST
  • Error when mapping in ggmap with API key (403 Forbidden)
  • Detect which script causes server overload - apache + php
  • run Rmpi on cluster, specify library path
  • How to know what r is doing behind the scene
  • GitLab CI Runner, how to use volumes or mounts in service containers
  • Automatic email sending with timer control
  • Can I add columns in a QListView in Qt?
  • UIPopoverController for iPhone
  • jQuery hover effect image preview going out of the browser's window
  • UpdateException when using SQL Server Compact with Entity Framework
  • SyntaxError: Unexpected token ' in JSON at position 1
  • Cassandra eats memory
  • Get process output without blocking
  • Need to display iframe when link is clicked from menu
  • Getting p-values from leave-one-out in R
  • Pseudo-element CSS dynamically using JavaScript
  • Vim syntax highlighting
  • R: add a line to contour plot
  • In Ember.js, what's the difference between store save and store commit?
  • Why can't pass only 1 coulmn to glmnet when it is possible in glm function in R?
  • Matlab Generating a Matrix
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • I don't get what's the difference between format() and … (python)
  • How to define an array of floats in Shader properties?
  • Scala: Function returning an unknown type
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?