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 Chimera3D · Nov 08, 2012 at 07:08 AM · aipathfindingclasses

AI Code Errors

I'm super noob when it comes to classes and stuff, so in-depth explanations may be necessary. I have an AI code that I haven't tested yet, I just wrote it. What I am trying to do is get a simple A* pathfinding system worked out. I'm not sure why I'm getting errors but it's probably something simple. I have a class which handles the f score, heuristics, and what not. First of all I'm getting errors saying that the class variables, when being used in another script, are not members of the gameobject class??? Secondly I'm getting errors when I try to assign the vars of a class (in another script) that the vars are unknown identifiers, again I have never been successful with code like this. Lastly I get an errors saying that I need an instance of my navigation script for it to work, I have an idea of what this might mean but would rather just get answers instead of messing up some more if I'm wrong.

So I would like help with fixing these three problems and it would be great if someone could check my AI code to make sure I'm doing everything right, what I'm missing, what needs to go, and what needs to be fixed. Thanks in advance, I posted the scripts below.

Player Code (This is where the navigation script is being called [hopefully I'm using the correct terminology here]):

 import System.Collections; 
 import System.Collections.Generic; 
 
 var desiredPosition : Vector3;
 
 private var start : NavCell;
 private var destination : NavCell;
 private var cells : ArrayList;
 
 function Update(){
 
         var points : NavCell = Navigation.CalcPath(start, destination, cells);
 
 }
  
 
 function PopulateCells () {
 
     var objects : GameObject[] = GameObject.FindGameObjectsWithTag("NavigableCells");
     
     for(var i : int = 0; i < objects.Length; i++){
         
         var cell = new NavCell();
         cell.originalGO = object;
         cell.position = originalGO.transform.position;
         cell.neighbors = originalGO(Cell).nearCells;
         cells.Add (cell);
         
         if(Vector3.Distance(transform.postion, cell.position) < 1.5){
         
             start = cell;
             
         }else
         
         if(Vector3.Distance(transform.position, desiredPosition) < 1.5){
         
             end = cell;
             
         }
         
     }
     
 }

NavCell Class (This is the NavCell class file):

 public class NavCell {
 
   var position: Vector3;
 
   var f: int;
   
   var h : int;
 
   var g : int;
 
   var originalGO: GameObject;
   
   var neighbors : GameObject[];
   
   var accessible : boolean;
   
   var pathParent : NavCell;
   
   public function SetPathParent(parent : NavCell, getG : int){
      
      pathParent = parent;
      g += getG;
      f = g + h;
      
   }
 
   public function GetHueristic (posA : Vector3, posB : Vector3) {
 
      h = Mathf.RoundToInt(Vector3.Distance(posA, posB));
     
   }
 
 }

Navigation Script (where stuff happens):

 import System.Collections;
 import System.Collections.Generic;
 
 public function CalcPath(from : NavCell, to : NavCell, cells : ArrayList) : NavCell[]
 {
     for (var n : NavCell in cells) n.SetPathParent(null, 0);
     
     var fp : NavCell = from;
     var openList : ArrayList  = new ArrayList();
     var closeList : ArrayList = new ArrayList();
 
     openList.Add(fp);
 
     while (openList.Count > 0)
     {
         // find one with lowest F score
         fp = null;
         for (var cl : NavCell in openList)
         {
             if (fp == null) fp = cl;
             else if (cl.f < fp.f) fp = cl;
         }
 
         // drop it from open list and add to close list
         closeList.Add(fp);
         openList.Remove(fp);
 
         if (fp == to) break; // reached destination, break
 
         // find neighbours of this point
         for (var i : int = 0; i < fp.neighbors.Length; i++)
         {            
                 // in close set
             if (closeList.Contains(fp.neighbors[i])) continue;
                 
             if (openList.Contains(fp.neighbors[i]))
             {    // open list contains the neighbour
                 // check if G score to the neighbour will be lower if followed from this point
                 var destG : int = fp.g;
                 if (destG < fp.neighbors[i].g)
                 {
                     destG = Mathf.RoundToInt(Vector3.Distance(fp.neighbors[i].position, to.position));
                     fp.neighbors[i].SetPathParent(fp, destG);
                 }
             }
             else
             {    // add neighbour to open list 
                 var distConst = Mathf.RoundToInt(Vector3.Distance(fp.neighbors[i].position, to.position));
                 fp.neighbors[i].SetPathParent(fp, fp.g + distConst);
                 openList.Add(fp.neighbors[i]);
                 }
             }
 
 
         // start at dest and build path
         var path : ArrayList = new ArrayList();
         fp = to;
         while (fp.pathParent != null)
         {
             path.Add(fp);
             fp = fp.pathParent;
         }
 
         if (path.Count > 0)
         {    // the path is calculated in reverse, swap it around
             // and then return the array of the elements
             path.Reverse();
             return path.ToArray(typeof(NavCell));
         }
         return null;
     }
 }
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 Chimera3D · Nov 09, 2012 at 08:57 PM 0
Share

any help??

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Loius · Nov 09, 2012 at 09:29 PM

--First of all I'm getting errors saying that the class variables, when being used in another script, are not members of the gameobject class

a;slkfhasklfhas. That's because they're not. If you have a "GameObject X", then it can access only GameObject members. You need to have a "MyClass X" to access members of MyClass. Without an indication of what line is giving the error (and you get the line number with the error, so no reason not to include that when asking for help...) I can only recommend that you check your return types to be sure you're actually given an object of the right type.

Unknown identifier is similar. It means you're trying to use something which hasn't been declared, like doing:

class Y { function X() { z+= 2; } }

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 Chimera3D · Nov 09, 2012 at 09:58 PM 0
Share

One particular part where I get that error is line 42 of the navigation code.

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

10 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

Related Questions

A* Pathfinding AIFollow in JavaScript? 0 Answers

NavMesh.SamplePosition returns point outside the NavMesh 0 Answers

How do you create an AI that works like this ? 1 Answer

Navigation dynamic level generation 1 Answer

Possible to edit the movement of NavMeshAgent ? 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