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 access tableviewcell using tableview instance
Self-Hosted Web service no loading
Merging data from two tables into one view
FreeLookCam script with a different “world up”?
Errors using GAC_MSIL assemblies on VSTS
.Net Core ClientWebSocket: Unable to connect to server
iTextSharp PDF Footer not visible in Adobe Reader
Mongodb Update Embedded Documents Using Multiple Field Matches with C# driver
Dynamic Polymorphism's weird output
When I select the adapter settings, then the app is getting squeezed after rotating the touch pad
directive or an assembly reference missing
How do I get ContentItems from ContentType using DNN?
Multiple disposable objects (Dispose objects before losing scope) [duplicate]
Fade-In effect for new items of ListView
Securing cookies in ASP.NET
How to align dynamically created textboxes?

Categories

HOME
actionscript-3
spring-boot
classification
redux
botframework
pyspark
cors
ssh
mysqli
azure-eventhub
casting
blob
skypedeveloper
esxi
msbi
fetch
responsive
brightway
beacon
project-structure
handle
scrapy-spider
inspec
windows-mobile
scripting-language
sphinx4
recreate
2checkout
edsdk
wordpress-rest-api
tikz
jsonapi-resources
webclient
bootcamp
ponylang
rythm
nsdateformatter
julia-jump
ui-grid
socketcluster
redbean
android-graphview
electron-packager
java-5
php-mongodb
spring-cloud-bus
monaca
serialversionuid
cancan
enet
playframework-2.3
aws-rds
dynatable
wicketstuff
breadcrumbs
boost-range
konakart
geokit
transaction-isolation
abstract
app-data
recode
jts
php-gd
self-join
windows-phone-8-emulator
exchange-server-2007
tinymce-plugins
gcc-warning
parseexception
beagleboard
shunting-yard
qxorm
xts
webshim
azure-caching
cvi
fade
pechkin
leap-year
latex-suite
quit
cryptarithmetic-puzzle
doxia
jquery-1.8
sup
sqlbuddy
stl-algorithm
maven-eclipse-plugin
session-hijacking
html5-animation
llblgen
for-xml
script-tag
html-help-workshop
cross-cutting-concerns

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