x


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!

more ▼

asked Apr 19 '11 at 09:57 AM

jamie gravatar image

jamie
31 12 12 16

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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");
}
more ▼

answered Apr 19 '11 at 10:03 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

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

Cheers.

Apr 19 '11 at 10:12 AM jamie

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

Apr 19 '11 at 10:25 AM Marowi

Awesome, thanks. Makes a lot of sense now that I look at it.

Apr 19 '11 at 10:32 AM jamie

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

Apr 19 '11 at 10:44 AM Marowi

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

Apr 19 '11 at 10:55 AM jamie
(comments are locked)
10|3000 characters needed characters left

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! :)

more ▼

answered Apr 19 '11 at 10:24 AM

FrHaYwOrKs gravatar image

FrHaYwOrKs
271 4 6 19

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.

Apr 19 '11 at 10:31 AM jamie

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

Apr 19 '11 at 10:35 AM FrHaYwOrKs
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2170
x336
x212
x127

asked: Apr 19 '11 at 09:57 AM

Seen: 610 times

Last Updated: Apr 19 '11 at 09:57 AM