Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Bryangs · Nov 22, 2016 at 09:06 AM · c#randomizebetweenchoose

Ramdomly select a variable

I am making a game which involves combat, and i wanted to make my enemy wander around. The best option that i have found was to store some empty gameobjects on some variables and make my enemy choose between them and follow one, but i don't know how to ramdomize it. How can i make my enemy choose between different variables? Something like this:

     public GameObject location1;
     public GameObject location2;
     public GameObject location3;
     public GameObject location4;
     public GameObject location5;
     public GameObject location6;
 
     void Start()
     {
         StartCoroutine(FollowCoordinates());
     }
 
     void Update()
     {
 
     }
 
     //This will set the target position for the NavmeshAgent from time to time.
     IEnumerator FollowCoordinates()
     {
 
         while (true)
         {
             //This will set the target coordinates
             GetComponent<NavMeshAgent>().SetDestination(?????.position); // The "?????" is where i want to make the random choice.
             //Wait one second and then loop
             yield return new WaitForSeconds(1);
         }
     }
 }

Sorry for the giant amount of variables, i am new to C-Sharp.

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 JatMidGrade · Nov 22, 2016 at 04:19 PM 0
Share

Sorry, was replying with a "correct" answer, but misread your code. Please don't hold it against me, haha.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by brunocoimbra · Nov 22, 2016 at 12:05 AM

Instead of using that amount of variables, use arrays:

      public GameObject[] locations;
 
      void Start()
      {
          StartCoroutine(FollowCoordinates());
      }
  
      IEnumerator FollowCoordinates()
      {
  
          while (true)
          {
              GetComponent<NavMeshAgent>().SetDestination(locations[Random.Range(0, locations.length)].transform.position); 
              yield return new WaitForSeconds(1);
          }
      }
  }
Comment
Add comment · Show 3 · 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 Bryangs · Nov 22, 2016 at 02:38 PM 0
Share

I am getting an error: " EnemyWander.cs (18,57): error CS0178: Invalid Rank specifier: expected ',' or ']' "

avatar image brunocoimbra Bryangs · Nov 22, 2016 at 03:59 PM 0
Share

Sorry, edited my post with the missing character...

avatar image Bryangs brunocoimbra · Nov 23, 2016 at 06:27 PM 0
Share

Now i am getting even more errors, it's painful to see :( alt text

moreerrors.png (14.9 kB)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Endless runner randomised scrolling terrain: don't want first terrain when scene loads to be randomised 0 Answers

How to call Random.insideUnitSphere.x Once 1 Answer

How do I compare if a variable of integer type is between two values 1 Answer

What to use instead of GameObject.Find and GetComponent 2 Answers

I have 12 objects using the same script below, I need them to each activate individually of each other. 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