- Home /
Problem is not reproducible or outdated
Unity is recording all info gathered from separate pathfinders into a single array. Why? (JS)
Here's my code:
function OnTriggerEnter(other:Collider)
{
if(other.tag=="pathfinder")
{
Debug.Log("I hit the pathfinder");
//Variables for potential rapid single use
var clone:Rigidbody;
var moveDistance:int;
//Copies pathfinder's history data to an internal variable for the waypoint
tempMyPathList=other.GetComponent("Pathfinder").myPathList;
tempMyPathList.Add(this.gameObject);
moveDistance=other.GetComponent("Pathfinder").distance;
//Checks if it's the last waypoint within moving distance
if(moveDistance>0)
{
//If it's not the last, it subtracts 1 from current value to give to next Pathfinder
moveDistance=moveDistance-1;
/*
Checks to see which waypoint surrounding this one
sent the Pathfinder in order to avoid sending a
pathfinder backwards. Whichever direction is
determined as the sending waypoint, this waypoint
will not send a pathfinder towards it.
*/
if(forwardWaypoint.tag=="waypoint" && other.GetComponent("Pathfinder").originWaypoint!=forwardWaypoint)
{
//Creates a new Pathfinder, changes it's name, assigns it variables, and sends it out in the forward direction
clone=Instantiate(pathfinder, forwardSpawner.transform.position, forwardSpawner.transform.rotation);
clone.name=this.gameObject.name+"PathfinderForward";
clone.GetComponent("Pathfinder").myPathList=tempMyPathList;
clone.GetComponent("Pathfinder").originWaypoint=this.gameObject;
clone.GetComponent("Pathfinder").distance=moveDistance;
clone.velocity=transform.TransformDirection(Vector3.forward*75);
}
if(rightWaypoint.tag=="waypoint" && other.GetComponent("Pathfinder").originWaypoint!=rightWaypoint)
{
//Creates a new Pathfinder, changes it's name, assigns it variables, and sends it out in the right direction
clone=Instantiate(pathfinder, rightSpawner.transform.position, rightSpawner.transform.rotation);
clone.name=this.gameObject.name+"PathfinderRight";
clone.GetComponent("Pathfinder").myPathList=tempMyPathList;
clone.GetComponent("Pathfinder").originWaypoint=this.gameObject;
clone.GetComponent("Pathfinder").distance=moveDistance;
clone.velocity=transform.TransformDirection(Vector3.right*75);
}
if(leftWaypoint.tag=="waypoint" && other.GetComponent("Pathfinder").originWaypoint!=leftWaypoint)
{
//Creates a new Pathfinder, changes it's name, assigns it variables, and sends it out in the left direction
clone=Instantiate(pathfinder, leftSpawner.transform.position, leftSpawner.transform.rotation);
clone.name=this.gameObject.name+"PathfinderLeft";
clone.GetComponent("Pathfinder").myPathList=tempMyPathList;
clone.GetComponent("Pathfinder").originWaypoint=this.gameObject;
clone.GetComponent("Pathfinder").distance=moveDistance;
clone.velocity=transform.TransformDirection(Vector3.left*75);
}
if(backWaypoint.tag=="waypoint" && other.GetComponent("Pathfinder").originWaypoint!=backWaypoint)
{
//Creates a new Pathfinder, changes it's name, assigns it variables, and sends it out in the back direction
clone=Instantiate(pathfinder, backSpawner.transform.position, backSpawner.transform.rotation);
clone.name=this.gameObject.name+"PathfinderBack";
clone.GetComponent("Pathfinder").myPathList=tempMyPathList;
clone.GetComponent("Pathfinder").originWaypoint=this.gameObject;
clone.GetComponent("Pathfinder").distance=moveDistance;
clone.velocity=transform.TransformDirection(-Vector3.forward*75);
}
}
else
{
/*
If this is the last waypoint withing moving
distance, this waypiont will render a particle
system to signify that it is a valid move.
*/
moveParticles.renderer.enabled=true;
for(var value in tempMyPathList)
{
Debug.Log(value);
}
Debug.Log(tempMyPathList.Count);
}
Destroy(other.gameObject);
}
}
So all the variables are working properly, they're recording and everything. The code you see is sitting on my waypoint, the pathfinder object it's looking for has just a few variables in it and that's it.
My problem is that at the end, I have a function that logs the value in the myPathList variable and somehow it's recorded all of the waypoints that all of the pathfinders hit. It should be that each waypoint is holding a single list of waypoints that the pathfinders have followed.
I feel like it has something to do with the instantiate code somehow creating another instance of the same object so every time a pathfinder hits something, it gets recorded on a single list.
I hope this is all making sense, I'd be happy to elaborate if I'm not clear on something. Just been banging my head against a wall for the past week trying different things and I'm stuck.
Thanks ahead of time for the help! -Kaze-
I'm going to rephrase this question, this thread is dead now.
Follow this Question
Related Questions
A* nodes in the basic version of unity. 1 Answer
AI Pathfinding Script 4 Answers
Need to change direction of transform? 2 Answers
Implicit downcast and ArrayList 1 Answer
Path finding using Raycasting inside of Unity(JavaScript) 1 Answer