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

Updating the key object in AddOrUpdate method
ObjectDisposedException while using socket
How can I add a text box dynamically under an other by pressing a button in c#? (winforms)
VS2013 Dynamically create chart c#
.UpdateLayout() throwns an exception from RunWorkerCompleted method
Unix to Dos Conversion in C#
How to fill a textbox by using another textbox textchanged event?
NHibernate How do i put data in many to many mapping
Is there a way to use the Windows Form table in a Razor view cshtml?
Round robin pub/sub with StackExchange.Redis
Check if 'Restrict background data' is active on Windows Phone
XMLHttpRequest: Network Error Access is denied
MongoDB EC2 C# Upload Issues - Missing Key?
ASP.Net Web API Validation Attributes on DTO?
Microsoft Solver Foundation could not solve my model in some cases
How to change the column names of a mapping table?

Categories

HOME
javafx
indexing
stanford-nlp
mysqli
intellisense
azure-database-mysql
stdout
ebay
uml-designer
richtextbox
osx-yosemite
ndepend
unreal-engine4
javascript-debugger
opendaylight
react-select
css-float
draw2d
ecmascript-2017
scalatest
knapsack-problem
windows-mobile
git-submodules
ibm-bpm
node-soap
repeat
campaign-monitor
scip
ui-grid
ssdp
file-descriptor
aspnetboilerplate
superclass
reqif
npoco
hdpi
cloudconvert
openidm
riemann
try-catch-finally
chain
was
huawei
distcc
instaparse
csc
database-administration
usps
pebble-js
jcalendar
skbio
wordpress-plugin
dnvm
cyberduck
nmock
stereotype
vectordrawable
autopep8
teamstudio-unplugged
cordova-3
system.io.packaging
vundle
nspopupbutton
reactjs-native
iwork
connections
wpa
junction-table
gwt2
ftpwebrequest
zepto
configurationsection
cryptarithmetic-puzzle
openafs
django-1.4
nsundomanager
umfpack
virtual-printer
session-hijacking
flexbuilder
nbehave
n2
cinder
rpxnow
data-formats
hotfix

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