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 Seventh Stealth · Dec 28, 2014 at 12:17 AM · javascriptaiupdatestealth

How do I get a function to recognize a variable in function Update()

 //SCRIPT GOAL
 //every 10 seconds pick a location,move to it and then patrol
 
 var ready : boolean;
 var speed : float;
 var timer : float;
 var patrolTimer : float = 10; //how long should it patrol for
 
 
 //Targets
 var target1 : Transform;
 var target2 : Transform;
 var target3 : Transform;
 var target4 : Transform;
 
 
 
 function Update (){
 
 if(ready == true){
 patrolTimer = 10;
 //pick location
 var chooseNext = Random.Range(0,2);
 //countdown to move 
 timer -= Time.deltaTime;
     if(timer <= 0){
     ready = false;
     }
 }
 
 if(ready == false){
 Move();
 }
 
 }
 
 
 function Move(){
 
 var step = speed * Time.deltaTime;
 
 if(chooseNext == 0){
 Debug.Log("moving");
 
 // move towards target1
         transform.position = Vector3.MoveTowards(transform.position, target1.position, step);
         if(this.gameObject.transform.position == target1.position){
         Patrol();
         Debug.Log("Reached destination 1");
         }
 }
         
         
         if(chooseNext == 1){
 Debug.Log("moving");
 
 // move towards target1
         transform.position = Vector3.MoveTowards(transform.position, target2.position, step);
         if(this.gameObject.transform.position == target2.position){
         Patrol();
         Debug.Log("Reached destination 2");
         }
         }
         
         if(chooseNext == 2){
 Debug.Log("moving");
 
 // move towards target1
         transform.position = Vector3.MoveTowards(transform.position, target3.position, step);
         if(this.gameObject.transform.position == target3.position){
         Patrol();
         Debug.Log("Reached destination 3 ");
         }
         
 }
 }
 
 
 
 function Patrol(){
 //patrolling
 Debug.Log("patrolling");
 patrolTimer -= Time.deltaTime;
 
   if(patrolTimer <=0){
   //finished patrolling
              ready = true;
              Timer = 1;
              Debug.Log("Ready to move");
              
            }
 }

Debugger keeps giving unknown identifier : chooseNext. I want chooseNext to change everytime ready becomes true so I can't put it into move() otherwise the ready = false function would keep calling it andd the object would spaz out deciding where to go. How can I get the function Move() section to get the chooseNext variable from update().

Thanks in advance Stealth

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NoseKills · Dec 28, 2014 at 01:33 AM

You make it a "field" instead of a local variable, i.e. you move it outside the Update() like your other variables. Then it's visible in the whole script.

 var chooseNext : int;
  
  function Update ()
  {
      if(ready == true)
      {
          patrolTimer = 10;
          //pick location
          chooseNext = Random.Range(0,2);
          //countdown to move 
          timer -= Time.deltaTime;
          if(timer <= 0)
          {
              ready = false;
          }
      }

      if(ready == false)
      {
          Move();
      }
  }

Also, I don't know what exactly you want to happen in this script and i don't know what the start values for time and ready are but to me it looks like that when Patrol() finishes for the first time, the next entire second you spend in the if(ready == true) randomising the chooseNext etc...

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 Seventh Stealth · Dec 28, 2014 at 07:31 PM 1
Share

Hey thanks man! Thats exactly what I want to happen. The script worked. Thanks again!

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

27 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

Related Questions

Stealth AI Script. If less than or equal to not working correctly. Any fixes ? 1 Answer

My Object only moves when the variable ready is true. Problem is that ready is only ever true while update is running. So to move the object the variable always has to be true. How can I make it move if the variable ready is false. 1 Answer

i can't set destination, using AI Navmesh via raycast 1 Answer

Player Health drops fast, how to pause after damage 0 Answers

Basic Enemy Javascript 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