- Home /
User resolved issue on their own.
&& comparison not executing in Javascript
I'm attempting to execute this script inside an OnTriggerEnter function:
if(forwardWaypoint.tag=="waypoint" && other.name!=forwardWaypoint.name+"Pathfinder")
{
clone=Instantiate(pathfinder, forwardSpawner.transform.position, forwardSpawner.transform.rotation);
clone.name=this.gameObject.name+"Pathfinder";
clone.velocity=transform.TransformDirection(Vector3.forward*.5);
}
It executes the first half of the if statement just fine but the it doesn't seem to take into account the second condition. What I'm trying to do is send a pathfinder down my waypoints, once a waypoint is hit, it destroys the pathfinder and spawns more. What I don't want it to do is spawn a pathfinder back the other direction it was just hit from.
So when each waypoint creates a pathfinder, it changes the pathfinder's name to include the name of the waypoint that created it. Now each waypoint already knows what waypoints are around it, so what I'm trying to do is have the waypoint that's being hit compare the name of the object that hit it to the data it already has about the waypoints. If it finds that the object that hit it is named *waypointname*Pathfinder, then it shouldn't spawn a pathfinder back that direction.
I have a similar issue in another part of my code, hopefully solving this problem will allow me to solve the other problem.
Thanks ahead of time for your help! -Kaze-
Add Debug.Log statements before the if to log forwardWaypoint.tag, forwardWaypoint.name, and other.name. $$anonymous$$ake sure they're the values you think they are.
Your Debug.Log should look like this:
Debug.Log("Are they equal?: '" + other.name + "' == '" + forwardWaypoint.name+"Pathfinder'");
Answer by clunk47 · Dec 20, 2012 at 12:41 AM
Try other.gameObject.name and forwardWaypoint.gameObject.name
If that doesn't work just post your full script and I'll work it out for you.
Btw all components share the name and tag of their owning gameobject. So when "other" is a Collider for example other.name and other.gameObject.name is the same thing as well as other.transform.name
my pathfinder object is a Rigidbody (as you can see in my script below)
so are we thinking it should be other.rigidBody.name? Because I was under the same impression as Bunny...
Hey Guys,
Thanks for your assistance. I found a way around my problem. Thanks for taking a look though! I'll definitely be back, sooner rather than later. How very fortunately unfortunate :)
-$$anonymous$$aze-
Hey man no problem, but if my answer didn't resolve the issue, you don't have to accept it. A thumbs up (vote up) for the effort would be more appropriate. I just don't want to lead others in the wrong direction if this answer didn't truly help :) I voted your question up for it being a good one, have a great day!
Answer by KazeEnji · Dec 20, 2012 at 02:30 AM
Here's my waypoint status script:
//Declares variables
var distance:float=30;
var forwardWaypoint:GameObject;
var rightWaypoint:GameObject;
var leftWaypoint:GameObject;
var backWaypoint:GameObject;
var forwardSpawner:GameObject;
var rightSpawner:GameObject;
var leftSpawner:GameObject;
var backSpawner:GameObject;
var pathfinder:Rigidbody;
function Awake ()
{
/*
Casts rays out in four directions to discover surrounding waypoints in a dynamic
environment.
*/
//Declares variables for internal use of function
var dirForward=transform.TransformDirection(Vector3.forward);
var dirRight=transform.TransformDirection(Vector3.right);
var dirLeft=transform.TransformDirection(Vector3.left);
var dirBack=transform.TransformDirection(-Vector3.forward);
var hitForward:RaycastHit;
var hitRight:RaycastHit;
var hitLeft:RaycastHit;
var hitBack:RaycastHit;
//Draws a colored line along the ray to be visually seen in the editor
Debug.DrawRay(transform.position, dirForward*distance, Color.blue);
Debug.DrawRay(transform.position, dirRight*distance, Color.green);
Debug.DrawRay(transform.position, dirLeft*distance, Color.yellow);
Debug.DrawRay(transform.position, dirBack*distance, Color.red);
//Detecting collisions in front of the waypoint
if(Physics.Raycast(transform.position, dirForward, hitForward, distance))
{
if(hitForward.collider.gameObject.tag=="waypointBlocker")
{
Debug.Log("You're hitting the waypointBlocker. Sweet!");
//Assigns the value of the collision to the public variable
forwardWaypoint=hitForward.collider.gameObject;
Debug.Log(forwardWaypoint+" is the value in the Forward Waypoint Variable.");
}
else if(hitForward.collider.gameObject.tag=="waypoint")
{
Debug.Log("You're hitting another waypoing. Awesome!");
//Assigns the value of the collision to the public variable
forwardWaypoint=hitForward.collider.gameObject;
Debug.Log(forwardWaypoint+" is the value in the Forward Waypoint Variable.");
}
}
//Detecting collisions to the right of the waypoint
if(Physics.Raycast(transform.position, dirRight, hitRight, distance))
{
if(hitRight.collider.gameObject.tag=="waypointBlocker")
{
Debug.Log("You're hitting the waypointBlocker. Sweet!");
//Assigns the value of the collision to the public variable
rightWaypoint=hitRight.collider.gameObject;
Debug.Log(rightWaypoint+" is the value in the Right Waypoint Variable.");
}
else if(hitRight.collider.gameObject.tag=="waypoint")
{
Debug.Log("You're hitting another waypoing. Awesome!");
//Assigns the value of the collision to the public variable
rightWaypoint=hitRight.collider.gameObject;
Debug.Log(rightWaypoint+" is the value in the Right Waypoint Variable.");
}
}
//Detecting collisions to the left of the waypoint
if(Physics.Raycast(transform.position, dirLeft, hitLeft, distance))
{
if(hitLeft.collider.gameObject.tag=="waypointBlocker")
{
Debug.Log("You're hitting the waypointBlocker. Sweet!");
//Assigns the value of the collision to the public variable
leftWaypoint=hitLeft.collider.gameObject;
Debug.Log(leftWaypoint+" is the value in the Left Waypoint Variable.");
}
else if(hitLeft.collider.gameObject.tag=="waypoint")
{
Debug.Log("You're hitting another waypoing. Awesome!");
//Assigns the value of the collision to the public variable
leftWaypoint=hitLeft.collider.gameObject;
Debug.Log(leftWaypoint+" is the value in the Left Waypoint Variable.");
}
}
//Detecting collisions behind the waypoint
if(Physics.Raycast(transform.position, dirBack, hitBack, distance))
{
if(hitBack.collider.gameObject.tag=="waypointBlocker")
{
Debug.Log("You're hitting the waypointBlocker. Sweet!");
//Assigns the value of the collision to the public variable
backWaypoint=hitBack.collider.gameObject;
Debug.Log(backWaypoint+" is the value in the Back Waypoint Variable.");
}
else if(hitBack.collider.gameObject.tag=="waypoint")
{
Debug.Log("You're hitting another waypoing. Awesome!");
//Assigns the value of the collision to the public variable
backWaypoint=hitBack.collider.gameObject;
Debug.Log(backWaypoint+" is the value in the Back Waypoint Variable.");
}
}
}
function OnTriggerEnter(other:Collider)
{
if(other.tag=="pathfinder")
{
Debug.Log("I hit the pathfinder");
var clone:Rigidbody;
if(forwardWaypoint.tag=="waypoint" && other.name!=forwardWaypoint.name+"Pathfinder")
{
clone=Instantiate(pathfinder, forwardSpawner.transform.position, forwardSpawner.transform.rotation);
clone.name=this.gameObject.name+"Pathfinder";
clone.velocity=transform.TransformDirection(Vector3.forward*.5);
}
if(rightWaypoint.tag=="waypoint" && other.name!=forwardWaypoint.name+"Pathfinder")
{
clone=Instantiate(pathfinder, rightSpawner.transform.position, rightSpawner.transform.rotation);
clone.name=this.gameObject.name+"Pathfinder";
clone.velocity=transform.TransformDirection(Vector3.right*.5);
}
if(leftWaypoint.tag=="waypoint" && other.name!=forwardWaypoint.name+"Pathfinder")
{
clone=Instantiate(pathfinder, leftSpawner.transform.position, leftSpawner.transform.rotation);
clone.name=this.gameObject.name+"Pathfinder";
clone.velocity=transform.TransformDirection(Vector3.left*.5);
}
if(backWaypoint.tag=="waypoint" && other.name!=forwardWaypoint.name+"Pathfinder")
{
clone=Instantiate(pathfinder, backSpawner.transform.position, backSpawner.transform.rotation);
clone.name=this.gameObject.name+"Pathfinder";
clone.velocity=transform.TransformDirection(-Vector3.forward*.5);
}
}
}
and here's my pathfinder script:
var distance:int=0;
var originWaypoint:GameObject;
function Awake()
{
distance=GameObject.FindWithTag("GlobalVariables").GetComponent("GlobalStateScript").moveDistance;
Debug.Log(distance+" is the value of the internal distance script.");
}
function OnTriggerEnter(other:Collider)
{
if(other.tag=="waypoint" && distance>0)
{
distance=distance-1;
GameObject.FindWithTag("GlobalVariables").GetComponent("GlobalStateScript").moveDistance=distance;
}
Destroy(this.gameObject);
}
The "&& distance>0" part of my pathfinder script is also having the same problem where it doesn't seem to be considered during execution.
Thanks for your help! -Kaze-