c#


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


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 or via c# script 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.

Related Links

C# WPF Select UserControl on Canvas
Is it possible to deal with multiple tables on single view with both side validation? ASP.NET MVC
How do I get the XML SOAP request of an WCF Web service request?
C# Meta-information for object serialisation
Adding a custom cursor in XNA/C#?
vbscript to c# - list groups a computer is memberof (retrieve array)
find out how a person is feeling? [closed]
WPF Custom Control's ToolTip MultiBinding problem
Need to define control flow and functionalities
C# extension methods not working as expected - see example
What happens when a Task is running and it's window is closed?
How to change ComboBox's Background Property during Mouse Hover
How to check a request is from local host with ASP.NET technic
Simple repository for .Net 2.0
Excel file generated from WinForm app via template is sometimes “InVisible”
Dictionary.FirstOrDefault() how to determine if a result was found

Categories

HOME
spring-boot
optimization
botframework
primefaces
aix
postsharp
azure-database-mysql
boxplot
scapy
olap
email-attachments
mongodb-replica-set
mbed
browsermob
css-float
web-parts
virtuemart
ajaxcontroltoolkit
android-navigation-drawer
typedef
onclicklistener
aws-iot
advertising
ecmascript-2017
jira-zephyr
proof-general
r-factor
multilanguage
akamai
opencsv
mplab
spring-oauth2
qt-designer
favorites
certbot
intersection
android-databinding
form-fields
julia-jump
mixed-models
tapply
electron-packager
compiler-warnings
definition
firefox-developer-edition
python-venv
amazon-fire-tv
synchronized
remap
stanford-nlp-server
jcifs
wink
gige-sdk
business-catalyst
fileopendialog
easy68k
dbi
git-push
pebble-js
ideavim
wizard
fadein
dday
controlpanel
wiql
producer-consumer
taverna
transport
android-filterable
avrcp
p4python
playn
jad
google-experiments
sony-xperia
optimistic-locking
qxorm
os.walk
api-eveonline
zoneminder
livescript
nspopupbutton
xpand
yandex-api
enquire.js
sqldatasource
internet-radio
grunt-wiredep
ironworker
infinity.js
codeigniter-form-helper
junction-table
bho
auctex
biztalk-deployment
vline
wpml
google-email-audit-api
github-archive
reflexil
helicontech
apache-commons-email
pex-and-moles
for-xml
mdd
asp.net-1.1

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile