- Home /
Tagging object while instating not working.
Hello
I have a problem... I can't tag objects while instating them. I've done this before, but now it doesn't work.
Here's some code I use. I have changed the name of the variables, just to make it easier to interpret.
[RPC]
void SpawnPrefab(int id, int prefab) {
string prefabID = "Prefab" + id;
GameObject go = Instantiate(prefabs[prefab], new Vector3(0.0f, 0.0f, 0.0f), Quaternion.LookRotation(-Vector3.forward)) as GameObject;
go.tag = prefabID;
}
This is a similar sample of what I've used before, but now it doesn't work. How can I fix it (the instantiating works, but not the tagging)?
$$anonymous$$aybe try to run a test to see if your newly created tag exists exactly the same:
string prefabID = "Prefab" + id;
for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.tags.Length; i++) {
if (UnityEditorInternal.InternalEditorUtility.tags[i].Contains(prefabID)) {
print("Exist");
break;
}
}
I've tested this before, but I tested it again, and it says all tags exist.
I think this problem may have been caused by file corruption, so I will reimport all assets and hopefully it will work.
Answer by whebert · Mar 20, 2013 at 09:31 PM
Just set it directly on your game object.
var gameObject : GameObject;
gameObject.tag = "TagName";
You have to setup the tags in the Tag Manager first of course, otherwise you'll get an error.
I've done all this before, but it doesn't work. I guess I'll have to reimport my scripts. I've had similar issues before, and they were caused by file corruption.
When you say it doesn't work, do you mean that you get an error when trying to tag the GameObject, or that there is no error but you can't change the tag from the default of "Untagged"?
Odd. If it is still not working, post some code - perhaps there is something else going on...
Where is "playerID" defined? Do you mean to use "prefabID"?
Well, unless you've added all potential prefab IDs to the Tag $$anonymous$$anager (Prefab1, Prefab2, Prefab3, etc.), that won't work. If you have, it should work...
Answer by Freaking-Pingo · Mar 20, 2013 at 09:35 PM
It should not be that difficult, just instantiate your object as you normally would do. Store your GameObject in some variable, and then change the instantiated object there.
public GameObject myPrefab;
void Start()
{
GameObject myObject = Instantiate(myPrefab) as GameObject;
myObject.tag = "myTag";
}
If we are talking about an alternative constructor that takes a string as an argument for tagging, then I think you are out of luck.
Let's say I want to set the tag to "string myString = "myTag;". Will "myObject.tag = myString;" work or not?
Yes, as long as you've already added that tag to the Tag $$anonymous$$anager.
Yes it would work, but you must remember to add the tag in the tag manager.
You don't have to explain how to use this, since I have used this before, but now it suddenly stopped working. I only went here to check if the method is still working, or if they've added another method to tag an object when instantiating it. I guess the scripts are corrupted again... I will reimport the scripts and try again. I've had problems with corrupted scripts before, and reimporting them solved the problem.
Answer by Goidel · Mar 20, 2013 at 09:30 PM
You have to go to the inspector then you press the tag thing on the top left. Then you choose a tag.
You're welcome! ;)