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 /
avatar image
0
Question by SonicDirewolf · Feb 21, 2018 at 11:56 AM · vector3vector3.movetowards

Move towards not working with velocity in script

I have a player with rigidbody set to dynamic. The player jumps on platforms. At some certain time I want the player to move towards a gameobject using Vector3.MoveTowards. But the script doesn't work and gives a object not set to reference error.

Here's the script.

Transform ReviverBox;

 Rigidbody2D rb;
 
 void Start () {
         ReviverBox = GameObject.FindWithTag("Reviver").transform;
 }
 
 void Update () {
 
       if (GameManager.instance.reviveBool)
        {
 
             transform.position = Vector3.MoveTowards(transform.position, ReviverBox.position, ReviveSpeed * Time.deltaTime);    // this line gives the error.
        }
 
             movement = Input.GetAxis("Horizontal") * speed;
 }
 
 
 void FixedUpdate(){
         Vector2 velocity = rb.velocity;
         velocity.x = movement;
         rb.velocity = velocity;
     
     
     }
         
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Feb 21, 2018 at 12:10 PM

Debug the code.

What reference is missing? The ReviverBox? IF you have "object not set to reference error" most probably is not "Vector3.MoveTowards" problem, is a reference problem.

First, do as tutorail shows, create a float variable to calculate the step before using MoveTowards, and use this 2 debug.log before MoveTowards to check via console if everything is ok.

 Debug.Log (ReviverBox.position);
 Debug.Log (ReviveSpeed);
Comment
Add comment · Show 4 · 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 SonicDirewolf · Feb 21, 2018 at 12:12 PM 0
Share

This is the error that I'm getting. NullReferenceException: Object reference not set to an instance of an object

When I click on it it takes me to this line

   transform.position = Vector3.$$anonymous$$oveTowards(transform.position, ReviverBox.position, ReviveSpeed * Time.deltaTime); 
avatar image tormentoarmagedoom SonicDirewolf · Feb 21, 2018 at 12:14 PM 0
Share

This means when it tries to execute the command, one of the information you give can not be find, is null. Thats why you shoud use the Debug.Log to see which variable is miissing, if is some position, speed, or what.

avatar image SonicDirewolf · Feb 21, 2018 at 12:15 PM 0
Share

No problem I just fixed it! I had commented out something that wasn't supposed to be commeneted. Thank You. The above code is working perfect now.

avatar image tormentoarmagedoom SonicDirewolf · Feb 21, 2018 at 12:24 PM 0
Share

Accept the answer anyway please :D

avatar image
0

Answer by dmitriy-untilov · Feb 21, 2018 at 12:16 PM

"object not set to reference error" can be caused by incorrect initialization of ReviverBox. You can simply define ReviverBox as public in your script and drag and drop appropriate GameObject in the UnityEditor. Then you can cache transform value to local variable: var reviverBoxTransform = ReviverBox.transform;

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.

Follow this Question

Answers Answers and Comments

94 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

Related Questions

Unity2D - MoveTowards not working as planned 1 Answer

make enemy only follow along the X and Z axis 1 Answer

MoveTowards won't translate on the y axis and lerp leaves character jittering in mid-air 0 Answers

Getting a variable from a specific gameobject for another script 3 Answers

Unexpected behavior on Vector3.moveTowards for Player 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