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

How to set initial size in WPF Window docked in DockPanel
Writing to StorageFile concurrently
Can any one explain why does a single threaded program in .net (VS) spawn so many other threads? [duplicate]
UNC paths are not supported
OneWay DependencyProperty to ViewModel property binding
Delete folder(s) inside Azure Blob storage container
Bind single datagrid with two diff datasources (Stored Procedure output and ArrayList)
AvalonDock cannot disable floating
C# Read Simple Json String [duplicate]
Restful WCF adding message header to outgoing response
Find String with BeginsWith and EndsWith
SharpSNMP max-repetitions increase causes buffer size exception through GPRS
Log LogEventInfo is not writing the correct information
How to build a simple property selector expression in ef6
Getting selected item in Items control
C# - How do I get a user input for date and time

Categories

HOME
internet-explorer
javafx
primefaces
ns2
django-middleware
crash
datetimepicker
terminal
checkpoint
zabbix
sahi
software-collections
token
velocity
fastreport
android-notifications
jinja2
github-flavored-markdown
webseal
template10
samba
alert
scotty
include-path
hololens
google-drive-android-api
pumping-lemma
windowserror
mongoose-im
libxml2
launchd
dynamic-memory-allocation
osgi-bundle
language-detection
keil
circle
voyager
traitsui
android-databinding
iterm2
origami
aspose.pdf
libzip
f#-fake
google-chrome-storage
openxava
bioperl
node-java
htop
seyren
selection-sort
androiddesignsupport
opticalflow
blackberry
google-scholar
firebase-job-dispatcher
scikit-image
joystick
hdr
rowname
kik
mrunit
pebble-js
dday
android-contentresolver
adempiere
polarion
grails-3.0.9
system.diagnostics
jeet-grid
nexusdb
mathics
fiware-health
vundle
cocos2d-x-2.x
powershell-v1.0
javacompiler
sat4j
switchpreference
visual-studio-setup-proje
nativequery
mahara
scidb
static-class
android-library
proj4
outlook-object-model
coinbase-php
cloudmade
mail-gem
certificate-revocation
systrace
django-admin-tools
ruby-1.9
discussion-board
openwrap
nyromodal
pitch-shifting
hp-trim
appointment
ffdshow
jgrowl
office-2003

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App