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 Grge · Mar 17, 2016 at 04:25 PM · scripting problemfatal error

Unity crashing due to error in script fo AI.

I have been following a tutorial for AI scripting but from this point https://www.youtube.com/watch?v=3Dw5d7PlcTM#t=23m44s he makes a few minor changes to the script which should streamline the AI but instead it causes unity to crash. Looking online it may have to do with the "while" causing an endless loop but I am not sure. Thanks for the help looking into this.

Below is the script which run fine but is not streamlined. using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Diagnostics;

public class Pathfinding : MonoBehaviour {

 public Transform seeker, target;
 Grid grid;

 void Awake() {
     grid = GetComponent<Grid> ();
 }
     void Update(){
     if (Input.GetButtonDown ("Jump")) {
         FindPath (seeker.position, target.position);
     }
 }
 void FindPath(Vector3 startPos, Vector3 targetPos) {

     Stopwatch sw = new Stopwatch ();
     sw.Start ();

     Node starNode = grid.NodeFromWorldPoint (startPos);
     Node targetNode = grid.NodeFromWorldPoint (targetPos);

     List<Node> openSet = new List<Node> ();
     HashSet<Node> closedSet = new HashSet<Node> ();
     openSet.Add (starNode);

     while (openSet.Count > 0) {
         Node currentNode = openSet [0];
         for (int i = 1; i < openSet.Count; i ++) {
             if (openSet [i].fCost < currentNode.fCost || openSet [i].fCost == currentNode.fCost && openSet [i].hCost < currentNode.hCost) {
                 currentNode = openSet [i];
                 }
                 }
             openSet.Remove(currentNode);
         closedSet.Add(currentNode);

         if (currentNode == targetNode) {
             sw.Stop ();
             print ("Path found: " + sw.ElapsedMilliseconds + " ms");
             RetracePath(starNode,targetNode);
             return;
         }

         foreach (Node neighbour in grid.GetNeighbours(currentNode)) {
             if (!neighbour.walkable || closedSet.Contains(neighbour)) {
                 continue;
 
         }
     int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
     if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) {
         neighbour.gCost = newMovementCostToNeighbour;
         neighbour.hCost = GetDistance (neighbour, targetNode);
         neighbour.parent = currentNode;

                 if (!openSet.Contains (neighbour))
                     openSet.Add (neighbour);
             
         }
     }
 }

}

 void RetracePath(Node startNode, Node endNode) {
 List<Node> path = new List<Node>();
 Node currentNode = endNode;

 while (currentNode != startNode) {
     path.Add(currentNode);
     currentNode = currentNode.parent;
 }
 path.Reverse();

 grid.path = path;
     }
     int GetDistance(Node nodeA, Node nodeB){
         int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX);
         int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY);

         if (dstX > dstY)
             return 14*dstY + 10* (dstX-dstY);
         return 14*dstX + 10 * (dstY-dstX);
     }
 }

And below is the streamlined script which crashes unity and I have to shutdown unity using the task manager.

using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Diagnostics;

public class Pathfinding : MonoBehaviour {

 public Transform seeker, target;
 Grid grid;

 void Awake() {
     grid = GetComponent<Grid> ();
 }
     void Update(){
     if (Input.GetButtonDown ("Jump")) {
         FindPath (seeker.position, target.position);
     }
 }
 void FindPath(Vector3 startPos, Vector3 targetPos) {

     Stopwatch sw = new Stopwatch ();
     sw.Start ();

     Node starNode = grid.NodeFromWorldPoint (startPos);
     Node targetNode = grid.NodeFromWorldPoint (targetPos);

     Heap<Node> openSet = new Heap<Node> (grid.MaxSize);
     HashSet<Node> closedSet = new HashSet<Node> ();
     openSet.Add (starNode);

     while (openSet.Count > 0) {
         Node currentNode = openSet.RemoveFirst();
         closedSet.Add(currentNode);

         if (currentNode == targetNode) {
             sw.Stop ();
             print ("Path found: " + sw.ElapsedMilliseconds + " ms");
             RetracePath(starNode,targetNode);
             return;
         }

         foreach (Node neighbour in grid.GetNeighbours(currentNode)) {
             if (!neighbour.walkable || closedSet.Contains(neighbour)) {
                 continue;
 
         }
     int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);
     if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) {
         neighbour.gCost = newMovementCostToNeighbour;
         neighbour.hCost = GetDistance (neighbour, targetNode);
         neighbour.parent = currentNode;

                 if (!openSet.Contains (neighbour))
                     openSet.Add (neighbour);
                 else {
                     openSet.UpdateItem (neighbour);
                 }

                   }
         }
    }

}

 void RetracePath(Node startNode, Node endNode) {
 List<Node> path = new List<Node>();
 Node currentNode = endNode;

 while (currentNode != startNode) {
     path.Add(currentNode);
     currentNode = currentNode.parent;
 }
 path.Reverse();

 grid.path = path;
     }
     int GetDistance(Node nodeA, Node nodeB){
         int dstX = Mathf.Abs(nodeA.gridX - nodeB.gridX);
         int dstY = Mathf.Abs(nodeA.gridY - nodeB.gridY);

         if (dstX > dstY)
             return 14*dstY + 10* (dstX-dstY);
         return 14*dstX + 10 * (dstY-dstX);
     }
 }

The changes made to the script that works to make it into the script that does not is Line 27 (List changed to Heap as well as a few words added to the line.) Deleted lines 33-38 and made a small change to the end of line 32. Line 62 added "else" and line 63 added - openSet.UpdateItem(neighbour);

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 Grge · Mar 17, 2016 at 04:31 PM 0
Share

Update:

The changes made to the script that works to make it into the script that does not is Line 20 (List changed to Heap as well as a few words added to the line.) Deleted lines 26-31 and made a small change to the end of line 25. Line 53 added "else" and line 54 added - openSet.UpdateItem(neighbour);

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

53 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

Related Questions

SPACE SHOOTER SHOOTING SHOTS TUTORIAL HELP (UNITY 5 C#) 3 Answers

unity doesnt recognise quaternions 0 Answers

Gradually changing the light intensity by clicking the mouse 0 Answers

How can i make my spaceship to land automatic on the Base ? 1 Answer

How to move a kinematic rigidbody with touch? 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