x


Random object from GameObject.FindGameObjectsWithTag?

Hello. Basically I have a series of objects in my scene, all with the same tag, "White".

I want to randomly pick one from this series, and using GameObject.FindGameObjectsWithTag I managed to create a list of the said objects. This "list", as the Unity Script Reference calls it, is apparently not an array, because if I try to assign a variable to a random member of it it gives me an error.

listWhites = GameObject.FindGameObjectsWithTag("White");
var i = Random.Range(0,listWhites.length);
var theObject : GameObject = listWhites[i];

Gives me this error:

MissingFieldException: Field 'System.MonoType.listWhites' not found. Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory.ResolveMember () Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory.CreateGetter () Boo.Lang.Runtime.RuntimeServices.CreateGetSliceDispatcher (System.Object target, System.String name, System.Object[] args) Boo.Lang.Runtime.RuntimeServices+c_AnonStorey1A.<>m_11 () Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetSlice (System.Object target, System.String name, System.Object[] args) init.Generate () (at Assets/init.js:60) init.Start () (at Assets/init.js:21)

Any idea on how to do this? Thanks in advance, and sorry for the bad english

more ▼

asked Mar 03 '11 at 06:30 PM

Bumbaz gravatar image

Bumbaz
25 3 3 7

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

3 answers: sort voted first

Try this:

listWhites.Length
more ▼

answered Mar 03 '11 at 06:48 PM

efge gravatar image

efge
5.1k 5 14 38

That's definitely not the problem. If I try to print i, it actually gives me a random number between 0 and the number of objects in the list.

Mar 03 '11 at 07:06 PM Bumbaz
(comments are locked)
10|3000 characters needed characters left

This "list", as the Unity Script Reference calls it, is apparently not an array

The list is definitely an array. I would guess you're using the wrong type, but since you haven't included all of the relevant code, I can't say for sure. It's not Array, it's a GameObject array, as specified by the docs (look at the return type).

more ▼

answered Mar 03 '11 at 07:57 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

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

You must add this after declaring list whites "[]" Like shown below

listWhites[] = GameObject.FindGameObjectsWithTag("White");
var i = Random.Range(0,listWhites.length);
var theObject : GameObject = listWhites[i];
more ▼

answered Apr 18 '11 at 04:21 AM

Robert Engen gravatar image

Robert Engen
1

This is incorrect. You can't use symbols like that in variable names, nor is it necessary in any case.

Apr 18 '11 at 04:56 AM Eric5h5
(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:

x2086
x1362
x33

asked: Mar 03 '11 at 06:30 PM

Seen: 1895 times

Last Updated: Mar 03 '11 at 06:30 PM