Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MounirDev · Mar 23, 2020 at 05:05 PM · gameobjectunity 2dtag

Stop a game object from following an untagged object.

When making game object follow something with tag, but then untagg it while running, why it keeps follow it? even if i instantiate another object that was supposed to follow the previously tagged object, it just keep moving towards it. please how to get an object to stop or just follow another tagged object when the targed is no longer tagged ? thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Mar 23, 2020 at 05:25 PM

@MounirDev, There is no magical property that tells your object to follow an object with with a tag, or to stop following it if the tag changes. You have to set that in your movement code.

The problem you have is that when you object becomes utagged, your movement code is still doing the same thing. Instead you need it to do something different if the tag changes

One way to do it is for your code to follow this kind of logic:

 Keep a referece to the  oldTargetObject
 Keep a reference to the targetObject
 Movement loop begins here {
         if I am walking then targetObject tag = what I want
         else if I am running  targetObject tag = something else
         if targetObject tag = what I want
                  move to targetObject position
         else 
                  move to oldTargetObject position
 }

Comment
Add comment · Show 2 · 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 MounirDev · Mar 23, 2020 at 06:47 PM 0
Share

Thanks for answering @streeetwalker ! I'm not sure how to switch a target game object when untagg an object, this is the code i'm using : alt text

when the little home is full with one habitant, i untagg it but all the objects just stick to it like this : alt text

I tried using a list of objects but i didn't implement it well..

annotation-2020-03-23-193858.png (14.7 kB)
annotation-2020-03-22-150533.png (11.4 kB)
avatar image streeetwalker MounirDev · Mar 23, 2020 at 07:08 PM 0
Share

before you do anything you need code to deter$$anonymous$$e if you are walking or running. Where is that happening?

And, where is that code that does tagging or untagging? How are you doing that?

Not seeing the rest of your code, it seems like you're kind of going the long way around to find a solution to what really is a simple problem.

For one, as a general rule you should not use GameObject.Find - it is slow, and is kind of lazy way of coding. In the long game of learning to code in Unity it is better set up all of your game objects to begin with using either public variable references and drag them to your script inspector, or assign them to variables at the time you instance them. If you do that you don't need to check tags at all, just switch targets when you run or walk.

A better algorithm is:

 public GameObject chickenObject;  // drag to the inspector
 public GameObject dogObject; // drag to the inspector
 private GameObject moveTarget
 
 Update {
         code to check for walking or running, using keyboard or what?
         if walking moveTarget = chickenObject, 
         else if running moveTarget = dogObject
          
         transform.pos = $$anonymous$$oveFromTo( transform.pos, moveTarget );
           
  }

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

274 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why can't you add editor scripts to GameObjects even if the tag is EditorOnly? 0 Answers

instantiate prefab at every gameobject with tag 0 Answers

How to access an inactive gameObject with tag 2 Answers

Making a gameobject follow a player closely 1 Answer

How can i add multiple tags from script? 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