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

SQL function returns Wrong in c#
WCF Endpoint. Multiple Clients
C# preprocessor directive or conditional for differentiating between Visual Studio 2012 and 2010?
System.ServiceModel.Security.SecurityNegotiationException while trying to access a WCF service from a Windows Forms client application
Using resources outside of Xna's “Game” class
does not implement interface member
Converting data type nvarchar to int
C# make file unable to edit and delete
Undefinded Signs in WPF Datagrid
LDAP Connection to AD Very Slow, Timing Out
How to calculate with values from textboxes, progressbar and performancecounters? C#
Enabling CascadeOnDelete for EF Code First
UDP sending data, something i don´t quite understand
C# Play MP3 file using NAudio from Resource
passing values between separate classes
razor get not working

Categories

HOME
qlikview
http
javafx
indexing
itunesconnect
stanford-nlp
leaflet
ide
jenkins-pipeline
postsharp
flowtype
android-webview
phpexcel
aws-sdk-cpp
ndepend
dynamics-crm-2011
websharper
opendaylight
aggregate
appsettings
luis
spring-webflow
turn.js
coccinelle
onclicklistener
devexpress-mvc
aspxgridview
core-animation
priority-queue
dynamic-memory-allocation
uibarbuttonitem
angular-schema-form
pypy
ds-5
delphi-2010
apm
opencsv
qt-designer
ports
scip
kepserverex
client-server
jibx
origami
gridgain
niagara-ax
react.rb
confluent
spring-cloud-bus
google-cloud-shell
ear
printer-control-language
bipartite
apache-commons-cli
cedar-bdd
firebase-polymer
easy68k
csc
recurring
falcon
hfp
audiorecord
pyshark
innertext
jsondoc
wordpress-plugin
web-performance
gtkwave
controlpanel
play-json
jain-sip
log4cxx
avrcp
tinymce-plugins
esb-toolkit-2.1
screen-lock
genero
jad
iphone-6
apache-pivot
vundle
time-frequency
nest-initiative
serializer
winrun4j
opencobol
nodeunit
data-layers
boofcv
ardor3d
grunt-contrib-imagemin
static-class
caroufredsel
listpicker
blotter
feincms
volatility
netbiscuits
doxia
certificate-revocation
filedialog
bindinglist
datejs
maven-eclipse-plugin
xpolog
zipstream
nbehave
numerical-computing
morfik
bootstrap-protocol

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile