# Script in prefab C#

**URL:** https://discussions.unity.com/t/script-in-prefab-c/475700
**Category:** C# & .NET
**Created:** [May 8, 2012, 5:05pm UTC](https://discussions.unity.com/t/script-in-prefab-c/475700 "2012-05-08T17:05:12Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![junahi](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@junahi](https://discussions.unity.com/u/junahi)
#### Post date: [May 8, 2012, 5:05pm UTC](https://discussions.unity.com/t/script-in-prefab-c/475700/1 "2012-05-08T17:05:12Z")

</div>

Going to put this here too in case answer comes from here =)

Going to just simplify my promlemo here, so stay with me.

I create Script lets say:

public class number1 : MonoBehaviour {  
public int try1;  
void Update ()  
{  
try1 = Random.Range(0,12);  
}  
}

Now I make this component of CubePrefab, which has lets say 3 clones.

In another objects Script, I want to know all of these 3 numbers which I get, how do I do that?

I tried public number1 HereComesPrefab;

and after that I ask Debug.log(number1.try1);

but it only gives me 0 every time.

Now I know there is some serious issues with my logic here, but please, help me to make this work, I am sure it would help me later =)

---

<div class="post-metadata">

### Author: ![superpig](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/superpig/32/992213_2.png) [@superpig](https://discussions.unity.com/u/superpig)
#### Post date: [May 8, 2012, 5:17pm UTC](https://discussions.unity.com/t/script-in-prefab-c/475700/2 "2012-05-08T17:17:24Z")

</div>

First you need to retrieve all the clones of CubePrefab:

```auto
var allComponents = (CubePrefab[]) FindObjectsOfType(typeof(CubePrefab));

```

Then you can work through the list and check the number in each prefab:

```auto
foreach(CubePrefab prefab in allComponents)
   Debug.Log(prefab.try1);

```

---

<div class="post-metadata">

### Author: ![junahi](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@junahi](https://discussions.unity.com/u/junahi)
#### Post date: [May 8, 2012, 8:22pm UTC](https://discussions.unity.com/t/script-in-prefab-c/475700/3 "2012-05-08T20:22:12Z")

</div>

Sorry, but I suck with JS =(

Is there any way you could translate this to C#. And does this code actually give me start/updated values, or only 0 to everything?

---

<div class="post-metadata">

### Author: ![superpig](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/superpig/32/992213_2.png) [@superpig](https://discussions.unity.com/u/superpig)
#### Post date: [May 8, 2012, 8:23pm UTC](https://discussions.unity.com/t/script-in-prefab-c/475700/4 "2012-05-08T20:23:43Z")

</div>

That _is_ C#.

It gives you the current values at the time you loop through the components.

---

<div class="post-metadata">

### Author: ![junahi](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@junahi](https://discussions.unity.com/u/junahi)
#### Post date: [May 8, 2012, 9:29pm UTC](https://discussions.unity.com/t/script-in-prefab-c/475700/5 "2012-05-08T21:29:44Z")

</div>

Oh, sorry, always though var = JS…

Well still, this code wont work, it says I type or namespace name could not be found unity

just to clarify, my object Prefabs name which has the script in it is named Terrain1Prefab and scripts name is number1 as I said before…

and in case it matter, the Script where all this is going is inside prefab PelaajaPrefab.

And yes, I dragged Terrain1Prefab inside the inspector of Pelaajaprefab in the HereComesPrefab slot.

---

<div class="post-metadata">

### Author: ![superpig](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/superpig/32/992213_2.png) [@superpig](https://discussions.unity.com/u/superpig)
#### Post date: [May 8, 2012, 11:48pm UTC](https://discussions.unity.com/t/script-in-prefab-c/475700/6 "2012-05-08T23:48:25Z")

</div>

‘var’ is supported as of C# 3.0 (IIRC). It’s just a shorthand that saves you from having to write the type out, it means “figure the type out for me.”

Can you paste the _exact_ compile error you’re seeing?
