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

Analysis is switched off in re-sharper
Bank Application that Handles Exceptions
RefreshSection not working as expected
Persisting dynamic object in DynamoDB with .NET SDK
Retrieve Only Modified Files After Visual Studio Publish (NB : More of a knowledge gathering)
Kafka consumer groups and partitions
DB_SEC_E_AUTH_FAILED(0x80040E4D) C# can't open database more than once
How can I filter my listbox items with a combobox?
PDFSharp / Migradoc PDF Generation for 3k+ rows going extremely slow
Get Radio Type, Channel and Encryption from Managed Wifi API c#
How to create a DXDialog substitute with DXWindow
C++/C# : pipe WriteFile() is not writing data to receiver end until you close the pipe handle
Keyboard hook storing keys
Paths and shapes: how to implement those?
how can i bind the json data to labels in c#
App opens maximized when it should not

Categories

HOME
msbuild
ns2
cluster-analysis
grpc
glsl
dynamic
skypedeveloper
ebay-api
hiveql
ms-application-insights
css-selectors
angularjs-directive
aggregation-framework
psexec
cdn
wavefront
vsixmanifest
urllib2
aspectj
http-status-codes
react-select
openstack-horizon
windows-phone
cucumberjs
conditional-formatting
udeploy
summernote
aws-iot
scrapy-spider
priority-queue
orgchart
dynamic-memory-allocation
jboss-arquillian
deferred
akamai
machine-code
ragel
jibx
office-interop
icu
smooth-streaming
extjs3
vertex
playframework-2.3
intermediate-language
r-commander
opticalflow
senti-wordnet
msiexec
disconnect
hoare-logic
airwatch
patching
angularfire
opendolphin
newlib
recode
producer-consumer
javapackager
random-sample
dpkt
web-standards
valdr-bean-validation
gmaps4jsf
data-quality-services
backtrace
qxorm
fxmlloader
conditional-comments
ios-enterprise
dotcmis
ccmenuitem
parallel-port
traceur
enquire.js
netbeans-plugins
google-mirror-api
myrrix
qgraphicsscene
proj4
junction-table
nsautoreleasepool
gmail-imap
setcookie
quit
xcode4.6.3
nserror
facebook-winjs-sdk
code-organization
xpolog
mismatch
lightopenid
table-valued-parameters
large-data-volumes
helios
appointment
ffdshow
expander
j++

Resources

Encrypt Message