Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MOLB · Mar 20, 2013 at 09:24 PM · instantiateprefabobjecttag

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Mar 23, 2013 at 08:33 AM 0
Share

$$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;
    }
 }
avatar image MOLB · Mar 23, 2013 at 08:47 AM 0
Share

I've tested this before, but I tested it again, and it says all tags exist.

avatar image MOLB · Mar 23, 2013 at 10:30 AM 0
Share

I think this problem may have been caused by file corruption, so I will reimport all assets and hopefully it will work.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MOLB · Mar 21, 2013 at 07:07 AM 0
Share

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.

avatar image whebert · Mar 21, 2013 at 12:44 PM 1
Share

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

avatar image whebert · Mar 21, 2013 at 04:42 PM 1
Share

Odd. If it is still not working, post some code - perhaps there is something else going on...

avatar image whebert · Mar 22, 2013 at 08:13 PM 1
Share

Where is "playerID" defined? Do you mean to use "prefabID"?

avatar image whebert · Mar 23, 2013 at 03:35 AM 1
Share

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...

Show more comments
avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MOLB · Mar 20, 2013 at 09:56 PM 0
Share

Let's say I want to set the tag to "string myString = "myTag;". Will "myObject.tag = myString;" work or not?

avatar image whebert · Mar 20, 2013 at 09:59 PM 0
Share

Yes, as long as you've already added that tag to the Tag $$anonymous$$anager.

avatar image Freaking-Pingo · Mar 20, 2013 at 10:02 PM 0
Share

Yes it would work, but you must remember to add the tag in the tag manager.

avatar image MOLB · Mar 21, 2013 at 06:56 AM 0
Share

I've added it, and it still doesn't tag the object.

avatar image MOLB · Mar 21, 2013 at 07:03 AM 0
Share

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.

Show more comments
avatar image
-1

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! ;)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MOLB · Mar 21, 2013 at 07:05 AM 1
Share

Read the question before you try to answer it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scripts of instantiated objects 1 Answer

assign different values to prefabs on instantiation 2 Answers

How to have unique instances of script on multiple instances of prefab? 1 Answer

Instantiate Problem 4 Answers

Multiple timers on server as objects or prefabs? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges