• 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 /
  • Help Room /
avatar image
1
Question by delphifirst · Feb 13, 2016 at 09:38 PM · gameobjecteditorprefabprefab-instance

Why does GameObject.FindWithTag() get a wrong instance?

Hello, everyone.

I drag a player prefab into the $$anonymous$$erarchy window to create a player instance. I name t$$anonymous$$s instance "Player" and tag it "Player".

When I try to get t$$anonymous$$s instance in my code, I find that GameObject.Find("Player") can get t$$anonymous$$s instance, but GameObject.FindWithTag("Player") get a wrong instance. I use Debug.log() to print each result, the first one is: Player, and the second one is: Player(Clone).

Where does t$$anonymous$$s Player(Clone) instance come from? Why does GameObject.FindWithTag() get the wrong instance?

Hope someone can help me.

Here is related code:

 void Start()
 {
 GameObject a = GameObject.Find("Player");
 GameObject b = GameObject.FindWithTag("Player");
 Debug.Log(a);
 Debug.Log(b);
 }
Comment
Add comment · Show 1
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 dan5071 · Feb 14, 2016 at 02:51 AM 0
Share

May we see your code that is causing the problem?

2 Replies

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

Answer by dan5071 · Feb 14, 2016 at 04:12 PM

I tried and was not able to replicate the error with the code that was given. The code you posted all looks perfectly valid to me, so I don't t$$anonymous$$nk it is actually your code that is causing the issue. Without seeing your project I can only guess, but I'd have to say one possibility is that you have another script that is instantiating a copy of the player, since t$$anonymous$$s is usually how you get a GameObject(Clone) instance in your scene. Do you have any scripts that are instantiating anyt$$anonymous$$ng? Even if you are not instantiating the player, it is possible that you accidentally changed the tag on a completely different game object, giving you two game objects with the "Player" tag. If you tried using GameObject.FindGameObjectsWithTag(), then it would return all the objects with the tag so you can see if there is more than one. I am not exactly sure what happens with GameObject.FindWithTag() when you have more than one object with the same tag, but I'd have to imagine it just picks the first one it finds.

Short Version: Test GameObject.FindGameObjectsWithTag() to see if you have more than one object with the "Player" tag.

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 delphifirst · Feb 15, 2016 at 03:22 AM 0
Share

Firstly, thank you for your answer.

After I asked this question, I continued to program other parts of my game and ignored this problem temporarily. After a while, the problem disappeared mysteriously (Both methods return the correct instance now). I cannot remember what I did clearly. Maybe I missed something.

If I ever come across this problem again, I will try your suggestions for debugging.

avatar image
0

Answer by gorbit99 · Feb 14, 2016 at 02:29 PM

Try flipping the two lines, so the findwithtag one is above the find one

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Scripted GameObjects vs Prefabs 0 Answers

Editor script: Variable created on gameObject in editor set to missing on runtime 0 Answers

How to instantiate Prefabs for an endless game? 0 Answers

Prefabs not turning blue and losing connection 6 Answers

Dragging prefab onto object through code 1 Answer


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