# asseign script to resources

**URL:** <https://discussions.unity.com/t/asseign-script-to-resources/460783>\
**Category:** Asset & Content Management\
**Created:** [November 14, 2011, 1:37am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783 "2011-11-14T01:37:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_9490287](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_9490287](https://discussions.unity.com/u/anon_9490287)\
**Post date:** [November 14, 2011, 1:37am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/1 "2011-11-14T01:37:15Z")

</div>

hello,  
How can I asseign a script to a prefab stored in a resource file.  
This prefab appears during the game so it’s not visible in my hierarchy in the scene view.  
I suspect that I can speak with this prefab using code but I don’t know how I can provide the prefab with code.

greetz

---

<div class="post-metadata">

**Author:** ![anon\_9490287](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_9490287](https://discussions.unity.com/u/anon_9490287)\
**Post date:** [November 14, 2011, 1:38am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/2 "2011-11-14T01:38:19Z")

</div>

During the game I get a clone of this prefab in the hirarchie listing

---

<div class="post-metadata">

**Author:** ![UnknownProfile](https://avatars.discourse-cdn.com/v4/letter/u/cab0a1/32.png) [@UnknownProfile](https://discussions.unity.com/u/UnknownProfile)\
**Post date:** [November 14, 2011, 1:43am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/3 "2011-11-14T01:43:01Z")

</div>

Use [GameObject.FindWithTag](http://unity3d.com/support/documentation/ScriptReference/GameObject.FindWithTag.html) or [GameObject.Find](http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html).

---

<div class="post-metadata">

**Author:** ![anon\_9490287](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_9490287](https://discussions.unity.com/u/anon_9490287)\
**Post date:** [November 14, 2011, 2:49am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/4 "2011-11-14T02:49:41Z")

</div>

I new that I could find him like that but how can I for example tell to my prefab that he had to play a audio source that is already asseigned to this prefab before he comes into the game. Cause if I put the code into my first person controller for example I get the message that there is no audiosource on the controller . That is offcourse not the meaning also, but how can I handel over the audiosource of the prefab I found ?

---

<div class="post-metadata">

**Author:** ![UnknownProfile](https://avatars.discourse-cdn.com/v4/letter/u/cab0a1/32.png) [@UnknownProfile](https://discussions.unity.com/u/UnknownProfile)\
**Post date:** [November 14, 2011, 2:59am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/5 "2011-11-14T02:59:04Z")

</div>

Just put an audio source onto the original prefab and set it to “Play on awake” or the option that looks similar.

---

<div class="post-metadata">

**Author:** ![anon\_9490287](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_9490287](https://discussions.unity.com/u/anon_9490287)\
**Post date:** [November 14, 2011, 3:09am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/6 "2011-11-14T03:09:33Z")

</div>

already thanks for your efforts ,

In my project if I walk over coins the prefab comes in the game,  
I have already asseigned audiosource in the original prefab and it stands on play on awake .  
Now, I have a few of that kind of prefabs in my scene. The raison why I wanted to now if I could code my prefab is because I want to shut down the music when my first person controller isn’t in a specific area around my prefab. Also I want to hang a gui text on this prefab so when my controller raycast the prefab , the gui pops up .

---

<div class="post-metadata">

**Author:** ![anon\_9490287](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_9490287](https://discussions.unity.com/u/anon_9490287)\
**Post date:** [November 14, 2011, 3:31am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/7 "2011-11-14T03:31:47Z")

</div>

Is it possible to give some hints how I approche this situation best ?

---

<div class="post-metadata">

**Author:** ![anon\_9490287](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_9490287](https://discussions.unity.com/u/anon_9490287)\
**Post date:** [November 14, 2011, 5:49am UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/8 "2011-11-14T05:49:55Z")

</div>

the strange thing is that raycast doesn’t seem to work with my prefabs, if I test the same code with other gameobjects it work well.  
Someone an explenation for that ?

thanks in advance

---

<div class="post-metadata">

**Author:** ![anon\_9490287](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@anon\_9490287](https://discussions.unity.com/u/anon_9490287)\
**Post date:** [November 14, 2011, 12:50pm UTC](https://discussions.unity.com/t/asseign-script-to-resources/460783/9 "2011-11-14T12:50:06Z")

</div>

this is the code I use for raycasting my prefab :

function Update(){

var hit2:RaycastHit;

if(Physics.Raycast(transform.position, transform.forward, hit2, 5))

{

if (hit2.collider.gameObject.name ==“mijn prefab(Clone)”)

{

print(“de deur”);

}

}
