• 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 BedrockLlama · Jan 08, 2016 at 06:12 PM · errortransformtargetinvokerepeatingtargeting

Constantly Targeting with an InvokeRepeating and Random Targeting

Hi, I have a script which in theory is fine, but there are lots of things that don't work to plan (not syntax errors, the script is fine). It is in C#. The main issue is that I want my object to target the target and follow it for 5 seconds until it resets, but it constantly follows the player, does anybody have any ideas why it would do this and how to stop it? The 2nd issue is that tr_player (the transform variable) always sets itself to my 5th target, so if anyone has any ideas about that it would be very helpful. This is my script:

     public Transform Bird;
 public Transform tr_Player;
 public float f_MoveSpeed = 25.0f, f_RotSpeed = 1000.0f;
 
 void Start () {

     tr_Player = GameObject.FindGameObjectWithTag ("Target").transform;

     InvokeRepeating("Reset",5, 5);

 }

 void Update () {

     transform.position += transform.forward * f_MoveSpeed * Time.deltaTime;
 
 }

 void Reset()
 {
     transform.position = Bird.position;

     tr_Player = GameObject.FindGameObjectWithTag ("Target").transform;

     transform.LookAt(tr_Player);

 }

}

Probably just a noob error but if anyone could help me with those two errors it would be really helpful!

Comment
Add comment · Show 4
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 vintar · Jan 09, 2016 at 08:35 PM 0
Share

For the second issue you mentioned your code : tr_Player = GameObject.FindGameObjectWithTag ("Target").transform; will go thorugh all objects and find the first one with the tag "Target", and it seems you have more than one. rather use:

GameObject[] targets = GameObject.FindGameObjectsWithTag ("Target");
tr_Player = targets[0]..transform;

avatar image BedrockLlama vintar · Jan 14, 2016 at 07:06 AM 0
Share

Thanks @vintar but that doesn't work, the double stop comes up as an error and when I remove it it comes up with pretty much everything else in red, any other ideas or am I doing something wrong?

avatar image Dave29483 · Jan 14, 2016 at 08:49 AM 0
Share

The line with the double full stops should be;

Int index = Random.Range(0, targets.Length); //If wanting a random target.

tr_Player = targets[index].GetComponent();

avatar image Dave29483 · Jan 14, 2016 at 08:51 AM 0
Share

Correction, GetComponent<Transform>();

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Targeting Script Errors 0 Answers

Transform does not exist in current context 1 Answer

Transform not updating 0 Answers

Make object move back and forth 2 Answers

Switching Target 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