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 jamie · Apr 19, 2011 at 09:57 AM · gameobjecttagissuefind

All the objects in my script choose the same target.

I have a script that is attached to a prefab, this prefab is spawned in the game world 10 times. The script tells the objects to choose a target with the tag 'Stone' and there are about 400 stones in the world. The thing is, they all choose the exact same stone, how do I make them truly choose a random one?

if(FreeRoam == true)
{
RandomTarget = gameObject.FindWithTag("Stone");
DisFromRTarget = Vector3.Distance(this.transform.position, RandomTarget.transform.position);
transform.LookAt(RandomTarget.transform);
Speed = Walk;
transform.position = Vector3.MoveTowards(transform.position, RandomTarget.transform.position,  Speed * Time.deltaTime);
animation.Play("Walk");
}

cheers!

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

2 Replies

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

Answer by · Apr 19, 2011 at 10:03 AM

Use GameObject.FindGameObjectsWithTag and then Random.Range using '0' and the length of the GameObject array.

Read through the documentation for both, and if you're still having trouble, let me know and I'll update my answer with further details.


Updated...

if(freeRoam == true)
{
    var allStones = GameObject.FindGameObjectsWithTag("Stone");
    randomTarget = allStones[Random.Range(0,allStones.length)];
    disFromRTarget = Vector3.Distance(this.transform.position, randomTarget.transform.position);
    transform.LookAt(randomTarget.transform);
    speed = walk;
    transform.position = Vector3.MoveTowards(transform.position, randomTarget.transform.position,  Speed * Time.deltaTime);
    animation.Play("Walk");
}
Comment
Add comment · Show 7 · 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 jamie · Apr 19, 2011 at 10:12 AM 0
Share

Gah i'm too tired to wrap my $$anonymous$$d around it, care to give me an example?

Cheers.

avatar image · Apr 19, 2011 at 10:25 AM 0
Share

Check the updated answer for (untested) code. As a side note, it's standard for variable names to be in lowerCamelCase, and function names are UpperCamelCase

avatar image jamie · Apr 19, 2011 at 10:32 AM 0
Share

Awesome, thanks. $$anonymous$$akes a lot of sense now that I look at it.

avatar image · Apr 19, 2011 at 10:44 AM 0
Share

No worries. Please remember to upvote any answers you find helpful, and click the tick if it solved your problem.

avatar image jamie · Apr 19, 2011 at 10:55 AM 0
Share

Hate to bother you again but it would seem the objects scroll through all the rocks ins$$anonymous$$d of just selecting one and sticking with it.

Show more comments
avatar image
1
Best Answer

Answer by FrHaYwOrKs · Apr 19, 2011 at 10:24 AM

Actually just calling you variable "RandomTarget" doesn't make your target random at all! :D

When you call FindWithTag function you get just the first element with that tag (I fought with that function just this morning! :D)

You should first get the whole array of GameObjects with the wanted tag, like this...

var myStones = GameObject.FindGameObjectsWithTag("Stone");

And then for every object which must point to one of those you can choose a random target...

var targetStone;
targetStone = Random.Range(0, myStones.length);

That should do the trick! :)

Comment
Add comment · Show 2 · 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 jamie · Apr 19, 2011 at 10:31 AM 1
Share

Yeh I know that variable names have no such effect (not that silly) but I tend to name variables about what I want to happen with them. Thanks for the help.

avatar image FrHaYwOrKs · Apr 19, 2011 at 10:35 AM 0
Share

I was just joking about the variable name, of course! :D

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

No one has followed this question yet.

Related Questions

Find all gameObjects with same tag 1 Answer

Find the first game object created with a given name 1 Answer

Changing visibility of objects with game tag 1 Answer

How do I find the closest target with a tag? c# 2 Answers

Whats the best way to access other Game Objects? 2 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