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 Kk1496 · Apr 21, 2014 at 01:15 AM · c#errorarrayswaypoint

Make NPC travel to waypoint

I've been trying to make an NPC travel through a series of waypoints and stop at the end of an array.

I started by making the NPC target a single waypoint and move toward int however, i receive these errors when I run the game:

NullReferenceException: Object reference not set to an instance of an object NPCMove.Start () (at Assets/Scripts/AI/NPCMove.cs:19)

UnassignedReferenceException: The variable target of 'NPCMove' has not been assigned. You probably need to assign the target variable of the NPCMove script in the inspector. UnityEngine.Transform.get_position () NPCMove.Update () (at Assets/Scripts/AI/NPCMove.cs:24)

 public class NPCMove : MonoBehaviour {
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
 
     private Transform myTransform;
     private Transform stopDistance;
 
     void Awake(){
         myTransform = transform;
     }
 
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("WayPoint");
         target = go.transform;
     }
     
     // Update is called once per frame
     void Update () {
         Debug.DrawLine(target.position, myTransform.position, Color.black);
 
         //look at
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
         //move
         myTransform.position += myTransform.forward *moveSpeed * Time.deltaTime;
     }

Also, how would I make this use an array.

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 xortrox · Apr 21, 2014 at 03:13 AM 0
Share

Does your WayPoint object exist at the time Start is called? Does it have the tag "WayPoint"?

1 Reply

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

Answer by Pecek · Apr 21, 2014 at 03:11 AM

There is no "gameObject.FindGameObjectWithTag", only "gameObject.FindWithTag". After that it should work(at least these errors should be gone).

To use an array you have to create one, and loop through it. You can get an array of GO's by

GameObject.FindGameObjectsWithTag()

Keep in mind this will return every GO with a certain tag which will break the system if you have multiple agents.

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 Kk1496 · Apr 22, 2014 at 07:28 AM 0
Share

I found this really old thread called "A Waypoint Script Explained in Super Detail!" and followed the instructions but it doesn't move past the first waypoint.

 public class NPC$$anonymous$$ove : $$anonymous$$onoBehaviour {
     public float accel = 0.8f;
     public float inertia = 0.9f;
     public float speedLimit = 10.0f;
     public float $$anonymous$$Speed = 1.0f;
     public float stopTime = 1.0f;
     public float rotationDamping = 6.0f;
     public bool smoothRotation = true;
     public Transform[] waypoints;
 
     private float currentSpeed = 0.0f;
     private int functionState = 0;
     private bool accelState;
     private bool slowState;
     private Transform waypoint;
     private int wpIndexPointer;
 
     void Start(){
         functionState = 0;
     }
 
     void Update(){
         if(functionState == 0){
             Accel();
         }
 
         if(functionState == 1){
             StartCoroutine(Slow());
         }
         waypoint = waypoints[wpIndexPointer];
     }
 
     void Accel(){
         if(accelState == false){
             accelState = true;
             slowState = false;
         }
 
         if(waypoint){
             if(smoothRotation){
                 Quaternion rotation = Quaternion.LookRotation(waypoint.position - transform.position);
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotationDamping * Time.deltaTime);
             }
         }
 
         currentSpeed = currentSpeed + accel * accel;
         transform.Translate(0, 0, currentSpeed * Time.deltaTime);
         
         if(currentSpeed >= speedLimit){
             currentSpeed = speedLimit;
         }
     }
 
     void OnTriggerEnter(){
         functionState = 1;
         wpIndexPointer++;
     }
 
     IEnumerator Slow(){
         if(slowState == false){
             accelState = false;
             slowState = true;
         }
 
         currentSpeed = currentSpeed * inertia;
         transform.Translate(0, 0, currentSpeed * Time.deltaTime);
 
         if(currentSpeed <= $$anonymous$$Speed){
             currentSpeed = 0.0f;
             yield return new WaitForSeconds(stopTime);
             functionState = 0;
         }
     }
 }

I also get an index out of range error.

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

22 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

Related Questions

Grabbing GameObjects and putting into Transform array? 1 Answer

IndexOutOfRangeException help on debug 3 Answers

Multiple Cars not working 1 Answer

Lists, Arrays and c# equivilent of js Array() 1 Answer

Scoring System ArgumentOutOfRangeException Error 2 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