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)?

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.

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.

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! :wink: