x


GameObject.Find("something")..how to use with transform,audiosource and texture ?

i use this code: GameObject.Find("something");

there's a way to use it with audiosource, transform or texture?

something like:

AudioSource.Find("something");

or

Transform.Find("something");

or

Texture.Find("something");

more ▼

asked May 11 '12 at 05:00 PM

gianni123 gravatar image

gianni123
64 16 20 27

ok, i find a way for transform:

GameObject.Find("something").transform;

i'm waiting for audiosource and texture

May 11 '12 at 05:32 PM gianni123

An audio source is attached to a GameObject, so you can just search for that GO.

May 11 '12 at 05:41 PM hijinxbassist

ok,but if i try to find the gameobject that is audiosource..

InvalidCastException: Cannot cast from source type to destination type.

May 11 '12 at 05:54 PM gianni123
var sourceGO=GameObject.Find("AudioSourceGO");
var mySource=sourceGO.GetComponent(AudioSource);

First you need to find the GameObject. Then get that GO's Component AudioSource

Mind posting the line(s) you are getting error(s) from, then we can explain whats wrong and how to remedy

May 11 '12 at 06:00 PM hijinxbassist

I do not need the audiosource object, but the object itself.

i've this script

var AudioObject:AudioSource;

function OnTriggerEnter(collision: Collider){

if (collision.tag == "Player"){

    Audiosource.audio.Play();
}

}

i want to change with another script the var AudioObject.

May 11 '12 at 06:36 PM gianni123
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It does not work that way, If you want to find components, you need to find the gameobject that has that specific component like the GUItexture,audiosource etc.. then get the component with "GetComponent" and then you can do stuff with it, you only find the objects that hold components, not the components themselves. (sorry for mentioning component so much)

Here is an example:

var Player: GameObject;

function Update(){

Player.FindWithTag ("YourObjectTag"); //Find with a tag

Player.getComponent (Component you need); //Get the component needed

}

To find transform you can use this: "Transform.Find"

If you want more info, type Find in the unity script reference.

Happy coding.

more ▼

answered May 11 '12 at 05:40 PM

Noah 1 gravatar image

Noah 1
1.1k 33 45 50

(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:

x2197
x2086
x1278
x208
x171

asked: May 11 '12 at 05:00 PM

Seen: 2028 times

Last Updated: May 12 '12 at 03:34 AM