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
0
Question by JayFitz91 · Sep 04, 2014 at 05:28 PM · coroutineboolconsole

Issue within my code (not sure with what exactly)

Can anyone point out what I'm doing wrong here:

 using UnityEngine;
 using System.Collections;
 
 public class moveTowardsPlayer : MonoBehaviour 
 {
     private Transform target;
     float speed = 30.0f; // move speed
     private bool magnetBool = false;
 
     void Start()
     {
         target = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
     void Update()
     {
         magnet ();
     }
 
     void magnet()
     {
         if(GUICounters.magnet == 1 && magnetBool == false)
         {
             magnetBool = true;
             Debug.Log ("here");
             transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
             StartCoroutine(magnetTimer());
         }
     }
 
     IEnumerator magnetTimer()
     {
         yield return new WaitForSeconds(5);
         GUICounters.magnet = 0;
         magnetBool = false;
         Debug.Log ("after coroutine");
     }
 }

So I want all objects with this script on it to go towards the player when I collect a certain amount of powerups (1 in this case) The problem is, when I collect one, they don't move towards the player, nothing happens.

In the console, when I collect a powerup, the Debug.Log("here") is displayed 3 times, even though it should only display once as I set the boolean to true inside the if statement.

The Coroutine is so the objects will move towards me for 5 seconds and then reset the counter back to 0 so I can repeat the process the next time I collect a powerup.

As far as I can see, this should work, but the strange behaviour from the console leads me to believe I'm missing something. If anyone could help me out, I'd appreciate it.

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 llnk7 · Sep 04, 2014 at 05:41 PM 0
Share

Hello, are you sure there's only one object with the script moveTowardsPlayer ? And if you don't call the coroutine does it still keep printing "here"?

1 Reply

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

Answer by AyAMrau · Sep 04, 2014 at 05:40 PM

Vector3.MoveTowards() has to be called repeatedly if you want the object to continue moving. Because you force the if statement to not execute more than once, then the object will only make one move (which may be very small so you can't see it) and stop.

You should probably make another coroutine that will call the line with MoveTowards every frame and then stop it once the time is up.

As for "here" appearing 3 times, do you have more than one object with this script?

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 JayFitz91 · Sep 04, 2014 at 05:53 PM 0
Share

Ah I see, that makes sense. I'll play around and see what I can come up with.

As for the "here" issue, that's my fault, I have multiple powerups and I've been so focused on this one I forgot I attached the script to the others too, that's my bad :P

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

24 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

Related Questions

Assertion Failed? 0 Answers

reset bool on timer passed by ref 1 Answer

Fnaf Rebot, not working with, Door. 1 Answer

Coroutine doesnt turn bool? 0 Answers

Coroutine sequence not running properly 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