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

Switching control backColor fast
How can a view fill a model?
Iterating form controls that are created at runtime
How to use a Resource Image in Word document?
detecting type of generics within generics
Stuck with httpwebrequest usage
ScriptManager Error in my code?
How to reduce the number of TCP ACK's during a highly reliable bulk transfer
Is this good practice for a Custom Exception?
Dynamically generate property getter/setter through reflection or similar
Ascending List Permutation
Getting Control level (view) validation errors back to the View Model
Displaying multiple pieces of information from a combobox selection?
Taskpool system for .NET
C# Remove padding from TabPages in Windows Forms
Serialization .txt vs .xml

Categories

HOME
airflow
sql-server-2014
domain-driven-design
cluster-analysis
azure-resource-manager
pandoc
autocad
hibernate-mapping
zabbix
install
ionic-native
olap
oms
apache-httpclient-4.x
urllib2
distinct
jinja2
sha1
jsfiddle
tag-cloud
powerpivot
vivado-hls
google-calendar-api3
hl7-fhir
bootstrap-datetimepicker
custom-component
fibonacci
priority-queue
expressionengine
traveling-salesman
orgchart
android-6.0-marshmallow
r-factor
xlib
ios9
andengine
kubectl
microstrategy
headphones
geocomplete
oracle-bpm-suite
office-interop
vertical-alignment
ape-phylo
head
weld
pyopenssl
dcom
skype-bots
px4
business-catalyst
breadcrumbs
x12
hoare-logic
mod
hana-sql-script
gtkwave
javapackager
lytro
dpkt
mergecursor
parseexception
numerical-recipes
backtrace
teamstudio-unplugged
fiware-health
launcher
clearcanvas
react-os
ccmenuitem
mvcgrid
banana-pi
image-preloader
lastinsertid
mobile-robots
aspnet-compiler
caroufredsel
qgraphicsscene
azure-scheduler
nx
reference-manual
blotter
metapost
report-viewer2010
netzke
optimistic-concurrency
datasheet
towerjs
datejs
yii-cactiverecord
file-processing
html5-animation
arraycollection
datadude
cons

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App