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 SirSmileX · Oct 21, 2016 at 08:42 AM · scripting problemnullreferenceexceptionscript error

Pathfindingscript NullReference error

Hey guys, this script should move an object to one transform after the other and then start at the start waypoint again after going through all Transforms in the array. However everytime the object reaches the last waypoint, a NullReference error occurs because targetWayPoint was not set to an instance of an object. I don't know how to solve this. Could someone help?

Script:

 public float speed;
 public int currentWayPoint;
 public Transform[] Waypoints;
 public bool FixedRotation;
 public bool Loop = true;

 private Quaternion FixedRotate;

 Transform targetWayPoint;

 void Start ()
 {
     this.transform.rotation = FixedRotate;
 }

 void Update ()
 {
     if (targetWayPoint == null)
         targetWayPoint = Waypoints [currentWayPoint];
     
     if (currentWayPoint < this.Waypoints.Length) 
     {
         Walk ();
     }

     if (FixedRotation) 
     {
         transform.rotation = FixedRotate;
     }
 }

 void Walk ()
 {
     if (this.transform.position == targetWayPoint.position) 
     {
         if (currentWayPoint == (Waypoints.Length - 1) && Loop) 
         {
             targetWayPoint = Waypoints [0];
         } 

         else if (currentWayPoint == (Waypoints.Length - 1) && !Loop) 
         {
             GetComponent<WaypointPathFinding> ().enabled = false;
         } 

         else 
         {
             currentWayPoint++;
             targetWayPoint = Waypoints [currentWayPoint];
         }
     }         

     transform.forward = Vector3.RotateTowards (transform.forward, targetWayPoint.position - transform.position, speed * Time.deltaTime, 0.0f);
     transform.position = Vector3.MoveTowards (transform.position, targetWayPoint.position, speed * Time.deltaTime);
 }
Comment
Add comment · Show 2
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 Landern · Oct 21, 2016 at 01:48 PM 0
Share

When you do reset your "currentWayPoint" index back to zero? Once reaching the last endpoint if feels like you should set that back to the initial waypoint index of 0, otherwise if the transform.position and targetwaypoint.position is still equal it will hit your last else statement on line 46 and increment the currentWayPoint out of the range of the index for the array. Just a thought.

avatar image SirSmileX Landern · Oct 22, 2016 at 07:49 AM 0
Share

In Line 33 the Script tests if the Object's Position is the same as the target's Position. Once it reached it the next if Statement is called which first tests whether the loop is Set to true and Checks if the current Waypoint is equal to the Waypoints array length $$anonymous$$us 1. If it's true the Code should Set the targetWaypoint back to the first Index of the Waypoints array. This should be the reset

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.

Follow this Question

Answers Answers and Comments

90 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

Related Questions

Android UnityPlayer attribute "currentActivity" returns null 1 Answer

NullExecption in a communication between two scripts 0 Answers

new scripts wont work, old scripts work fine. why? new scripts won't do ANY work. 0 Answers

NullReferenceException in FiniteStateMachine with ThirdPersonCharacter 0 Answers

Why do i get NullReferenceException here? 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