- Home /
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");
ok, i find a way for transform:
GameObject.Find("something").transform;
i'm waiting for audiosource and texture
An audio source is attached to a GameObject, so you can just search for that GO.
ok,but if i try to find the gameobject that is audiosource..
InvalidCastException: Cannot cast from source type to destination type.
var sourceGO=GameObject.Find("AudioSourceGO");
var mySource=sourceGO.GetComponent(AudioSource);
First you need to find the GameObject. Then get that GO's Component AudioSource
$$anonymous$$ind posting the line(s) you are getting error(s) from, then we can explain whats wrong and how to remedy
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.
Answer by Noah-1 · May 11, 2012 at 05:40 PM
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.
Your answer
Follow this Question
Related Questions
transform.Find() returns a Transform? 1 Answer
Finding class where current GameObject is assigned to as a var 1 Answer
Finding Children question 3 Answers
Finding object with transform 1 Answer
Find Transform in the scene 2 Answers