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 /
This question was closed Jul 24, 2013 at 10:47 PM by clunk47 for the following reason:

User resolved issue on their own.

avatar image
2
Question by KazeEnji · Dec 20, 2012 at 12:19 AM · javascriptcompare

&& 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-

Comment
Add comment · Show 3
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 Dave-Carlile · Dec 20, 2012 at 12:23 AM 1
Share

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.

avatar image Bunny83 · Dec 20, 2012 at 01:09 AM 1
Share

Your Debug.Log should look like this:

     Debug.Log("Are they equal?: '" + other.name + "' == '" + forwardWaypoint.name+"Pathfinder'");
avatar image KazeEnji · Dec 20, 2012 at 02:30 AM 1
Share

Thanks, I'll add that in a little bit.

2 Replies

  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · 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 Bunny83 · Dec 20, 2012 at 01:13 AM 2
Share

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

avatar image KazeEnji · Dec 20, 2012 at 03:04 PM 1
Share

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...

avatar image KazeEnji · Dec 20, 2012 at 08:44 PM 2
Share

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-

avatar image clunk47 · Dec 20, 2012 at 10:30 PM 1
Share

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!

avatar image
0

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-

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Sort Multiple Arrays By Their Length 1 Answer

function inside function or while loop (javascript) 1 Answer

obj location on txt file 1 Answer


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