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

Could not load file or assembly System.DirectoryServices. This assembly is built by a runtime newer than the currently loaded runtime [duplicate]
signalr Clients.Users(userIds).send(msg) not firing
ASP.NET Web Pages - Restrict download folder for all user except admin
can i change the column type of a DataTable after using data adapter to fill it
A breakdown of the syntax of the answer: accessing non-static method from another class without instantiate
C# MVC DropDownListFor: Set Selected In Array With OptGroup
Invoke Button in Excel Programatically C#
The contents of last column in a DataGridView gets squeezed instead of the just moving
C#: How to add certain elements from another list in another class and print out?
Azure Function - Event Hub to AWS RDS Postgres
Store entered values in c#
quartz-scheduler create auditing on JobSchedulerListener
Run a COM based c# console application from Windows Service (c#)
Getting stuck in a loop that slows down the application insanely
Change theme on all forms using Modern UI
Wed Mar 01 2017 invalid datetime string format

Categories

HOME
http
google-cloud-pubsub
display
dronekit
liquibase
x86-64
lithium
coordinates
yuv
swap
symfony-3.2
virtualhost
wkhtmltopdf
google-app-maker
desktop-application
watson-dialog
mbed
console-application
jsfiddle
brightway
css-float
samba
project-reactor
beacon
summernote
ampscript
gomobile
xna
my.cnf
jmsserializerbundle
jql
imdb
android-scrollview
openvms
bootcamp
http-proxy
leaderboard
hippocms
oracle-bpm-suite
yelp
framebuffer
word-wrap
scalamock
collation
ape-phylo
gradlew
html-entities
django-cors-headers
rivets.js
react.rb
openxava
aws-rds
qt3d
cedar-bdd
blackberry-webworks
turbolinks
firebase-polymer
mate-desktop
transaction-isolation
ewam
encode
matlab-engine
newlib
angular-promise
roslyn-code-analysis
polarion
vectordrawable
data-representation
connection-timeout
douglas-peucker
touch-typing
drools-guvnor
indic
azure-worker-roles
oxygenxml
nest-initiative
parallel-port
switchpreference
mvcgrid
sendy
connections
kuka-krl
nativequery
codio
lastinsertid
xml-validation
octokit
wpa
wpd
select2-rails
nx
boost.build
linqdatasource
feincms
configurationsection
openafs
google-email-audit-api
django-admin-tools
animationdrawable
mvcmailer
joomla1.7
cherokee
marathontesting
chars
text-manipulation
jquery-attributes
for-xml
atmega16
authkit
jettison

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