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 $$anonymous$$ · May 09, 2013 at 08:35 PM · c#buglogicthanks

Help in solving bug in my code :)

Ive wrote a simple code for example,im clicking at the spot with this code and transfrom cube is moving to spot, it works, but with a bug: when im clicking again at another spot2, while cube is moving to spot1,cube stops.Think problem is in logic of a code. Thanks for any help.

using UnityEngine; using System.Collections;

public class PlanetMoveTest : MonoBehaviour

{

 public Transform Cube;
 public float speed = 10f;
 bool isMoving = false;
 
 
 
 // Use this for initialization
 void Start () 
 {
 
 }
 
 void OnMouseDown()
 {
     isMoving = true;
     
 }
 
 
 // Update is called once per frame
 void Update () 
 {
     if(isMoving)
     {
                     
         Cube.transform.position = Vector3.MoveTowards(Cube.transform.position,transform.position,Time.deltaTime * speed);
         
     }
     
 
     
     if(Cube.transform.position == transform.position)
     {
          isMoving = false;
         
         
     }
     
 }

}

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 robertbu · May 09, 2013 at 08:52 PM 2
Share

There are a number of issues and puzzles here, and I'm not sure which (if any) account for the behavior you describe. First On$$anonymous$$ouseDown() will only be called if you click on the object you have this script attached to. So if you are clicking on anything other than the object this script is attached to, this script is not being called.

Or you might have this script attached to multiple objects (likely from your description) and expect it to move to the next object when a new object is clicked? Then you will have multiple objects fighting to make the cube move with the $$anonymous$$oveTowards(). That is both scripts will attempt to move the cube each frame. The fight might result in a stalemate and thus no movement. Note typically an object moves itself rather than being moved from outside. If the object moves itself, there would be no fighting, just a reassignment of the destination.

avatar image Brahim113 · May 09, 2013 at 09:25 PM 0
Share

What i think could be the problem is that it never is in the exact position as the one it is moving towards. I had similar problems a year ago and the solution is not to have it the exact same position.

avatar image robertbu · May 09, 2013 at 09:37 PM 0
Share

Don't think so. Put a Debug.Log() statement at line 35. I'm pretty sure it will fire as long as you don't click on a second object.

avatar image $$anonymous$$ · May 09, 2013 at 10:34 PM 0
Share

So what can you suggest?

1 Reply

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

Answer by robertbu · May 10, 2013 at 01:51 AM

As mentioned above, I'd restructure the code so that the follower moved itself. I know nothing about the nature of your game, but here are a couple of simple scripts as proof of concept. The first goes on the follower and it must be named "Follower" (or you can change the name in the code):

 public class Follower : MonoBehaviour {
     
     public Vector3 v3Dest;
     public float speed = 5.0f;
 
     void Start () {
         v3Dest = transform.position;
     }
     
     void Update () {
         transform.position = Vector3.MoveTowards(transform.position,v3Dest,Time.deltaTime * speed);
         transform.LookAt(v3Dest);
     }
 }


And this script goes on any leaders (i.e. the things you click on to change the destination):

 public class Leaders : MonoBehaviour {
 
     private Follower follower;
     
     void Start () {
         follower = GameObject.Find ("Follower").GetComponent< Follower >();
     }
     
     void OnMouseDown() {
         follower.v3Dest = transform.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 $$anonymous$$ · May 10, 2013 at 08:26 AM 0
Share

I think logic of your code is ok, but it gave me two errors,this when im starting game : NullReferenceException at follower = GameObject.Find ("Follower").GetComponent< Follower >(); and second when im clicking at object,NullReferenceException: Object reference not set to an instance of an object at follower.v3Dest = transform.position;

avatar image $$anonymous$$ · May 10, 2013 at 10:34 AM 0
Share

Forgot to rename my object to "Follower" on the scene; Problem solved.Question may be deleted.

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Ride across a tree? 1 Answer

Trying to make gameobject follow mouse cursor. It disappears instead. 2 Answers

Line Renderer Positioning Problem 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